10
10
11
11
This package provide typed arrays for php as extension of native [ ArrayObject] ( http://php.net/manual/en/class.arrayobject.php ) .
12
12
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
-
17
13
## Requirements
18
14
This package require php 7.
19
15
@@ -27,36 +23,45 @@ composer require linna/typed-array
27
23
``` php
28
24
use Linna\TypedArray;
29
25
30
- //correct, only int passed to array .
26
+ //correct, only int passed to constructor .
31
27
$array = new TypedArray('int', [1, 2, 3, 4]);
28
+
29
+ //correct, int assigned
32
30
$array[] = 5;
33
- //throw InvalidArgumentException.
31
+
32
+ //throw InvalidArgumentException, string assigned.
34
33
$array[] = 'a';
35
34
36
- //throw InvalidArgumentException.
35
+ //throw InvalidArgumentException, mixed array passed to constructor .
37
36
$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;
44
37
45
- //correct, only Foo class instances passed to array .
38
+ //correct, only Foo class instances passed to constructor .
46
39
$array = new TypedObjectArray(Foo::class, [
47
40
new Foo(),
48
41
new Foo()
49
42
]);
43
+
44
+ //correct, Foo() instance assigned.
50
45
$array[] = new Foo();
51
- //throw InvalidArgumentException.
46
+
47
+ //throw InvalidArgumentException, Bar() instance assigned.
52
48
$array[] = new Bar();
53
49
54
- //throw InvalidArgumentException.
50
+ //throw InvalidArgumentException, mixed array of instances passed to constructor .
55
51
$array = new TypedObjectArray(Foo::class, [
56
52
new Foo(),
57
53
new Bar()
58
54
]);
59
55
```
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
+
60
65
## Performance consideration
61
66
Compared to the parent class [ ArrayObject] ( http://php.net/manual/en/class.arrayobject.php ) typed arrays are slower on writing
62
67
approximately from 6x to 8x. The slowness is due to not native ` __construct() ` and not native ` offsetSet() ` .
@@ -74,4 +79,4 @@ $arrayElement = $array[0];
74
79
$elements = $array->count();
75
80
```
76
81
![ 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