ddruganov / php-typed-array
Typed array support for php
Installs: 10
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/ddruganov/php-typed-array
Requires (Dev)
README
Typed array support for php
Installation
composer require ddruganov/php-typed-array
How-to
Use TypedArray
to create a container of a certain type:
final class SomeClass { public TypedArray $typedArray; public function __construct() { $this->typedArray = TypedArray::of(TypeDescription::of(TypeDescription::INT)); } }
Unfortunately, something like this is entirely possible:
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)); } }
To counter that, define a class extending the typed array:
final class DummyArray extends TypedArray { public function __construct() { parent::__construct(new TypeDescription(DummyClass::class)); } public function offsetGet(mixed $offset): DummyClass { return parent::offsetGet($offset); } }
You're good to go :)
Merging
$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);