This package provide typed arrays for php as extension of native ArrayObject.
This package require php 7.1
With composer:
composer require linna/typed-array
use Linna\TypedArray;
//correct, only int passed to constructor.
$intArray = new TypedArray('int', [1, 2, 3, 4]);
//correct, int assigned
$intArray[] = 5;
//throw InvalidArgumentException, string assigned.
$intArray[] = 'a';
//throw InvalidArgumentException, mixed array passed to constructor.
$otherIntArray = new TypedArray('int', [1, 'a', 3, 4]);
//correct, only Foo class instances passed to constructor.
$fooArray = new TypedArray(Foo::class, [
new Foo(),
new Foo()
]);
//correct, Foo() instance assigned.
$fooArray[] = new Foo();
//throw InvalidArgumentException, Bar() instance assigned.
$fooArray[] = new Bar();
//throw InvalidArgumentException, mixed array of instances passed to constructor.
$otherFooArray = new TypedArray(Foo::class, [
new Foo(),
new Bar()
]);Note: Allowed types are: array, bool, callable, float, int, object, string and all existing classes.
Compared to first version of the library, this version is a bit slower because after merging TypedObjectArray with TypedArray,
there are more code that be executed when new instance is created and on assign operations.
Compared to the parent class ArrayObject typed arrays are slower on writing
approximately from 6x to 8x. The slowness is due to not native __construct() and not native offsetSet().
Other operations do not have a speed difference with the native ArrayObject.
use Linna\TypedArray;
//slower from 6x to 8x.
$array = new TypedArray('int', [1, 2, 3, 4]);
$array[] = 5;
//other operations, fast as native.
//for example:
$arrayElement = $array[0];
$elements = $array->count();
View the speed test script on gist.




