7
7
[ ![ Code Coverage] ( https://scrutinizer-ci.com/g/s3b4stian/linna-array/badges/coverage.png?b=master )] ( https://scrutinizer-ci.com/g/s3b4stian/linna-array/?branch=master )
8
8
[ ![ StyleCI] ( https://styleci.io/repos/93407083/shield?branch=master&style=flat )] ( https://styleci.io/repos/93407083 )
9
9
10
- ## Typed arrays for php
10
+ # Typed arrays for php
11
11
Provide typed arrays for php as extension of native [ ArrayObject] ( http://php.net/manual/en/class.arrayobject.php )
12
12
13
13
## Requirements
@@ -19,36 +19,46 @@ With composer:
19
19
composer require s3b4stian/linna-array
20
20
```
21
21
22
- ## Usage
23
- Valid for intArray, stringArray and floatArray classes
22
+ ## Usage TypedArray
24
23
``` php
25
- use Linna\intArray ;
24
+ use Linna\TypedArray ;
26
25
27
- //int array
28
26
//correct, only int passed to array.
29
- $intArray = new intArray( [1, 2, 3, 4]);
30
- $intArray [] = 5;
27
+ $array = new TypedArray('int', [1, 2, 3, 4]);
28
+ $array [] = 5;
31
29
32
- //throw TypeError.
33
- $intArray = new intArray([1, 'a', 3, 4]);
34
- $intArray[] = 'a';
30
+ //throw InvalidArgumentException.
31
+ $array = new TypedArray([1, 'a', 3, 4]);
32
+ $array[] = 'a';
33
+ ```
34
+ Allowed types are: array, bool, callable, float, int, object, string.
35
+ ``` php
36
+ use Linna\TypedArray;
37
+
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');
35
46
```
36
47
37
- ### Performance consideration
48
+ ## Performance consideration
38
49
Compared to the parent class [ ArrayObject] ( http://php.net/manual/en/class.arrayobject.php ) typed arrays are slower on writing
39
50
approximately from 15x to 20x. The slowness is due to not native ` __construct() ` and not native ` offsetSet() ` .
40
51
Other operations do not have a speed difference with the native ArrayObject.
41
52
``` php
42
- use Linna\intArray ;
53
+ use Linna\TypedArray ;
43
54
44
55
//slower from 15x to 20x.
45
- $intArray = new intArray( [1, 2, 3, 4]);
46
- $intArray [] = 5;
56
+ $array = new TypedArray('int', [1, 2, 3, 4]);
57
+ $array [] = 5;
47
58
48
59
//other operations, fast as native.
49
60
//for example:
50
- $arrayElement = $intArray[0];
51
- $elements = $intArray->count();
52
- $array = (array) $intArray;
61
+ $arrayElement = $array[0];
62
+ $elements = $array->count();
53
63
```
54
64
0 commit comments