1
|
|
|
<?php |
|
|
|
|
2
|
|
|
require_once dirname(__DIR__) . '/vendor/autoload.php'; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class Item |
6
|
|
|
* |
7
|
|
|
* A simple item. |
8
|
|
|
*/ |
9
|
|
|
class Item |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
public $position; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ItemCollection |
16
|
|
|
* |
17
|
|
|
* A simple collection of items. |
18
|
|
|
*/ |
19
|
|
|
class ItemCollection extends \RQuadling\TypedArray\TypedArray |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
const ARRAY_TYPE = 'Item'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Collections |
26
|
|
|
* |
27
|
|
|
* An example abstract collections class that implements an each() method. |
28
|
|
|
*/ |
29
|
|
|
abstract class Collections extends \RQuadling\TypedArray\TypedArray |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @param callable $callable |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function each(callable $callable) |
36
|
|
|
{ |
37
|
|
|
foreach ($this as $key => $value) { |
38
|
|
|
$callable($value, $key); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Class ItemCollectionEx |
47
|
|
|
* |
48
|
|
|
* A extended collection of items. |
49
|
|
|
*/ |
50
|
|
|
class ItemCollectionEx extends Collections |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
const ARRAY_TYPE = 'Item'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create a new item collection and add a second item to it. |
57
|
|
|
*/ |
58
|
|
|
$items = new ItemCollection([new Item]); |
59
|
|
|
$items[] = new Item; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create an extended collection and add a second item to it. |
63
|
|
|
*/ |
64
|
|
|
$itemsEx = new ItemCollectionEx([new Item]); |
65
|
|
|
$itemsEx[] = new Item; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Use the each() method to set the position property in the item. |
69
|
|
|
*/ |
70
|
|
|
$itemsEx->each(function(Item $item, $key){ |
71
|
|
|
$item->position = $key; |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Clone the extended items collection. |
76
|
|
|
*/ |
77
|
|
|
$itemsEx2 = clone $itemsEx; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Use the each() method to make further changes to the position property of |
81
|
|
|
* the cloned collection. |
82
|
|
|
*/ |
83
|
|
|
$itemsEx2->each(function(Item $item, $key){ |
84
|
|
|
$item->position += $key; |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Make a new simple collection using the same items as the cloned collection. |
89
|
|
|
*/ |
90
|
|
|
$items2 = new ItemCollection( $itemsEx2); |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Show the results of all of this work. |
94
|
|
|
* |
95
|
|
|
* Pay special attention to the object #'s. |
96
|
|
|
*/ |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Firstly the simple collection #2[#3, #4] |
100
|
|
|
*/ |
101
|
|
|
var_dump($items); |
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Secondly the extended collection #5[#6, #7] |
105
|
|
|
*/ |
106
|
|
|
var_dump($itemsEx); |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Thirdly, the cloned extended collection #8[#11, #12] |
110
|
|
|
*/ |
111
|
|
|
var_dump($itemsEx2); |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Finally, the copied simple collection #9[#11, #12] |
115
|
|
|
* |
116
|
|
|
* NOTE: These items are the same ones as in $itemsEx2. |
117
|
|
|
*/ |
118
|
|
|
var_dump($items2); |
119
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.