8
8
[ ![ StyleCI] ( https://styleci.io/repos/93407083/shield?branch=master&style=flat )] ( https://styleci.io/repos/93407083 )
9
9
10
10
# Typed arrays for php
11
- Provide typed arrays for php as extension of native [ ArrayObject] ( http://php.net/manual/en/class.arrayobject.php )
11
+ Provide typed arrays for php as extension of native [ ArrayObject] ( http://php.net/manual/en/class.arrayobject.php ) .
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.
12
16
13
17
## Requirements
14
18
This package require php 7.
@@ -28,23 +32,31 @@ $array = new TypedArray('int', [1, 2, 3, 4]);
28
32
$array[] = 5;
29
33
30
34
//throw InvalidArgumentException.
31
- $array = new TypedArray([1, 'a', 3, 4]);
35
+ $array = new TypedArray('int', [1, 'a', 3, 4]);
36
+ //throw InvalidArgumentException.
32
37
$array[] = 'a';
33
38
```
34
- Allowed types are: array, bool, callable, float, int, object, string.
39
+ Allowed types are: * array* , * bool* , * callable* , * float* , * int* , * object* , * string* .
40
+
41
+ ## Usage TypedObjectArray
35
42
``` php
36
- use Linna\TypedArray ;
43
+ use Linna\TypedObjectArray ;
37
44
38
- $intArray = new TypedArray('int');
39
- $arrayArray = new TypedArray('array');
40
- $boolArray = new TypedArray('bool');
41
- $callableArray = new TypedArray('callable');
42
- $floatArray = new TypedArray('float');
43
- $intArray = new TypedArray('int');
44
- $objectArray = new TypedArray('object');
45
- $stringArray = new TypedArray('string');
46
- ```
45
+ //correct, only Foo class instances passed to array.
46
+ $array = new TypedObjectArray(Foo::class, [
47
+ new Foo(),
48
+ new Foo()
49
+ ]);
50
+ $array[] = new Foo();
47
51
52
+ //throw InvalidArgumentException.
53
+ $array = new TypedObjectArray(Foo::class, [
54
+ new Foo(),
55
+ new Bar()
56
+ ]);
57
+ //throw InvalidArgumentException.
58
+ $array[] = new Bar();
59
+ ```
48
60
## Performance consideration
49
61
Compared to the parent class [ ArrayObject] ( http://php.net/manual/en/class.arrayobject.php ) typed arrays are slower on writing
50
62
approximately from 15x to 20x. The slowness is due to not native ` __construct() ` and not native ` offsetSet() ` .
0 commit comments