ddruganov / php-typed-array
此包的最新版本(1.0.5)没有可用的许可信息。
为php提供类型化数组支持
1.0.5
2022-05-04 11:40 UTC
Requires (Dev)
README
为php提供类型化数组支持
安装
composer require ddruganov/php-typed-array
如何使用
使用 TypedArray
创建特定类型的容器
final class SomeClass { public TypedArray $typedArray; public function __construct() { $this->typedArray = TypedArray::of(TypeDescription::of(TypeDescription::INT)); } }
不幸的是,类似以下内容是完全可能的
final class SomeClass { public TypedArray $typedArray; public function __construct() { $this->typedArray = TypedArray::of(TypeDescription::of(TypeDescription::INT)); } public function someMethod() { $this->typedArray = TypedArray::of(TypeDescription::of(TypeDescription::STRING)); } }
为了应对这种情况,定义一个扩展类型化数组的类
final class DummyArray extends TypedArray { public function __construct() { parent::__construct(new TypeDescription(DummyClass::class)); } public function offsetGet(mixed $offset): DummyClass { return parent::offsetGet($offset); } }
你可以开始了 :)
合并
$firstArray = new IntArray(); $firstArray[0] = 1; $firstArray[1] = 2; $secondArray = new IntArray(); $secondArray[0] = 3; $secondArray[1] = 4; $thirdArray = new IntArray(); $thirdArray[0] = 5; $thirdArray[1] = 6; $merged = IntArray::from($firstArray, $secondArray, $thirdArray);