1010
1111This package provide typed arrays for php as extension of native [ ArrayObject] ( http://php.net/manual/en/class.arrayobject.php ) .
1212
13- Package contain two classes:
14- ** TypedArray** for arrays that accept only values of the user defined type.
15- ** TypedObjectArray** for arrays that accept only class instances of the user defined type.
16-
1713## Requirements
1814This package require php 7.
1915
@@ -27,36 +23,45 @@ composer require linna/typed-array
2723``` php
2824use Linna\TypedArray;
2925
30- //correct, only int passed to array .
26+ //correct, only int passed to constructor .
3127$array = new TypedArray('int', [1, 2, 3, 4]);
28+
29+ //correct, int assigned
3230$array[] = 5;
33- //throw InvalidArgumentException.
31+
32+ //throw InvalidArgumentException, string assigned.
3433$array[] = 'a';
3534
36- //throw InvalidArgumentException.
35+ //throw InvalidArgumentException, mixed array passed to constructor .
3736$array = new TypedArray('int', [1, 'a', 3, 4]);
38- ```
39- > ** Note:** Allowed types are: * array* , * bool* , * callable* , * float* , * int* , * object* , * string* .
40-
41- ## Usage - TypedObjectArray
42- ``` php
43- use Linna\TypedObjectArray;
4437
45- //correct, only Foo class instances passed to array .
38+ //correct, only Foo class instances passed to constructor .
4639$array = new TypedObjectArray(Foo::class, [
4740 new Foo(),
4841 new Foo()
4942]);
43+
44+ //correct, Foo() instance assigned.
5045$array[] = new Foo();
51- //throw InvalidArgumentException.
46+
47+ //throw InvalidArgumentException, Bar() instance assigned.
5248$array[] = new Bar();
5349
54- //throw InvalidArgumentException.
50+ //throw InvalidArgumentException, mixed array of instances passed to constructor .
5551$array = new TypedObjectArray(Foo::class, [
5652 new Foo(),
5753 new Bar()
5854]);
5955```
56+
57+ > ** Note:** Allowed types are: * array* , * bool* , * callable* , * float* , * int* , * object* , * string* and all existing classes.
58+
59+ ## Performance consideration for v2.0
60+ Compared to first version of the library, this version is a bit slower because after merging ` TypedObjectArray ` with ` TypedArray ` ,
61+ there are more code that be executed when new instance is created and on assign operations.
62+
63+ ![ Array Speed Test] ( array-speed-test-v2.png )
64+
6065## Performance consideration
6166Compared to the parent class [ ArrayObject] ( http://php.net/manual/en/class.arrayobject.php ) typed arrays are slower on writing
6267approximately from 6x to 8x. The slowness is due to not native ` __construct() ` and not native ` offsetSet() ` .
@@ -74,4 +79,4 @@ $arrayElement = $array[0];
7479$elements = $array->count();
7580```
7681![ Array Speed Test] ( array-speed-test.png )
77- View the speed test script on [ gist] ( https://gist.github.com/s3b4stian/9441af5855b795cc1569b3cdb5e7526d ) .
82+ View the speed test script on [ gist] ( https://gist.github.com/s3b4stian/9441af5855b795cc1569b3cdb5e7526d ) .
0 commit comments