Skip to content

Commit 9a24c9d

Browse files
authored
README.md updated
1 parent 71bff1c commit 9a24c9d

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
[![StyleCI](https://styleci.io/repos/93407083/shield?branch=master&style=flat)](https://styleci.io/repos/93407083)
99

1010
# 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.
1216

1317
## Requirements
1418
This package require php 7.
@@ -28,23 +32,31 @@ $array = new TypedArray('int', [1, 2, 3, 4]);
2832
$array[] = 5;
2933

3034
//throw InvalidArgumentException.
31-
$array = new TypedArray([1, 'a', 3, 4]);
35+
$array = new TypedArray('int', [1, 'a', 3, 4]);
36+
//throw InvalidArgumentException.
3237
$array[] = 'a';
3338
```
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
3542
```php
36-
use Linna\TypedArray;
43+
use Linna\TypedObjectArray;
3744

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();
4751

52+
//throw InvalidArgumentException.
53+
$array = new TypedObjectArray(Foo::class, [
54+
new Foo(),
55+
new Bar()
56+
]);
57+
//throw InvalidArgumentException.
58+
$array[] = new Bar();
59+
```
4860
## Performance consideration
4961
Compared to the parent class [ArrayObject](http://php.net/manual/en/class.arrayobject.php) typed arrays are slower on writing
5062
approximately from 15x to 20x. The slowness is due to not native `__construct()` and not native `offsetSet()`.

0 commit comments

Comments
 (0)