1 | <?php |
||
8 | class Collection |
||
9 | { |
||
10 | /** |
||
11 | * Collection items. |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | private $items = []; |
||
16 | |||
17 | /** |
||
18 | * Implementation namespace. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | private $classname; |
||
23 | |||
24 | /** |
||
25 | * Create a new collection instance. |
||
26 | * |
||
27 | * @param string|null $classname Class or interface of namespace for implementation |
||
28 | */ |
||
29 | 57 | public function __construct($classname = null) |
|
33 | |||
34 | /** |
||
35 | * Add a new item to collection. |
||
36 | * |
||
37 | * @param string $name |
||
38 | * @param mixed $value |
||
39 | * |
||
40 | * @return self |
||
41 | */ |
||
42 | 57 | public function put($name, $value) |
|
52 | |||
53 | /** |
||
54 | * Verify's if the given object has a valid instance. |
||
55 | * |
||
56 | * @param object $object |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | 57 | private function isValidObjectInstance($object) |
|
68 | |||
69 | /** |
||
70 | * Check object instance. |
||
71 | * |
||
72 | * @param object $object |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | 57 | private function isInstanceOrSubclassOf($object) |
|
80 | |||
81 | /** |
||
82 | * Get all items from collection. |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | 3 | public function all() |
|
90 | |||
91 | /** |
||
92 | * Find collection item by name. |
||
93 | * |
||
94 | * @param string $name |
||
95 | * |
||
96 | * @return mixed|null |
||
97 | */ |
||
98 | 33 | public function find($name) |
|
104 | |||
105 | /** |
||
106 | * Check if the given item exists on collection. |
||
107 | * |
||
108 | * @param string $name |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | 42 | public function has($name) |
|
116 | |||
117 | /** |
||
118 | * Find a specific collection item or throw's an exception. |
||
119 | * |
||
120 | * @param string $name |
||
121 | * |
||
122 | * @return mixed |
||
123 | */ |
||
124 | 12 | public function findOrFail($name) |
|
134 | |||
135 | /** |
||
136 | * Run a callback over each item. |
||
137 | * |
||
138 | * @param callable $callback |
||
139 | * |
||
140 | * @return self |
||
141 | */ |
||
142 | 3 | public function each(callable $callback) |
|
152 | |||
153 | /** |
||
154 | * Remove an item from collection. |
||
155 | * |
||
156 | * @param string $name |
||
157 | * |
||
158 | * @return self |
||
159 | */ |
||
160 | 9 | public function remove($name) |
|
168 | } |
||
169 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.