1 | <?php |
||
14 | abstract class Collection extends \SplFixedArray |
||
15 | { |
||
16 | /** |
||
17 | * SplFixedArrayは固定長になってしまうため、自動伸長機能を追加 |
||
18 | * @param int $offset |
||
19 | * @param mixed $value |
||
20 | * @return void |
||
21 | */ |
||
22 | 5 | function offsetSet($offset, $value) |
|
|
|||
23 | { |
||
24 | 5 | if ($offset === null) { |
|
25 | 1 | $cnt = count($this); |
|
26 | 1 | $this->setSize($cnt + 1); |
|
27 | 1 | $offset = $cnt; |
|
28 | 1 | } |
|
29 | |||
30 | 5 | return parent::offsetSet($offset, $value); |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * SplFixedArray固定ではなく、継承した子クラスを返すように拡張 |
||
35 | * |
||
36 | * @param array $array 元にする配列 |
||
37 | * @param bool $save_indexes なるべく元の添え字を保持しようとするかどうか |
||
38 | * @return static |
||
39 | */ |
||
40 | 4 | static function fromArray($array, $save_indexes=true) |
|
41 | { |
||
42 | 4 | $splFixedArray = \SplFixedArray::fromArray($array, $save_indexes); |
|
43 | 4 | $cnt = count($array); |
|
44 | 4 | $self = new static($cnt); |
|
45 | 4 | foreach ($splFixedArray as $i => $v) { |
|
46 | 4 | $self[$i] = $v; |
|
47 | 4 | } |
|
48 | |||
49 | 4 | return $self; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * getterは許可しない |
||
54 | */ |
||
55 | 1 | function __get($name) |
|
59 | |||
60 | /** |
||
61 | * setterは許可しない |
||
62 | */ |
||
63 | 1 | function __set($name, $value) |
|
67 | } |
||
68 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.