1 | <?php |
||
8 | class Collection |
||
9 | { |
||
10 | /** |
||
11 | * Collection items. |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | private $items = []; |
||
16 | |||
17 | /** |
||
18 | 18 | * Implementation namespace. |
|
19 | * |
||
20 | 18 | * @var string |
|
21 | 18 | */ |
|
22 | 18 | 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 | public function __construct($classname = null) |
||
33 | |||
34 | 15 | /** |
|
35 | * Add a new item to collection. |
||
36 | 12 | * |
|
37 | * @param string $name |
||
38 | * @param mixed $value |
||
39 | * |
||
40 | * @return self |
||
41 | */ |
||
42 | public function put($name, $value) |
||
52 | |||
53 | /** |
||
54 | 3 | * Verify's if the given object has a valid instance. |
|
55 | * |
||
56 | * @param object $object |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | private function isValidObjectInstance($object) |
||
68 | |||
69 | /** |
||
70 | * Check object instance. |
||
71 | * |
||
72 | * @param object $object |
||
73 | * @return bool |
||
74 | 9 | */ |
|
75 | private function isInstanceOrSubclassOf($object) |
||
79 | |||
80 | 6 | /** |
|
81 | * Get all items from collection. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | public function all() |
||
89 | |||
90 | 6 | /** |
|
91 | * Find collection item by name. |
||
92 | 6 | * |
|
93 | * @param string $name |
||
94 | 6 | * |
|
95 | 3 | * @return mixed|null |
|
96 | */ |
||
97 | public function find($name) |
||
103 | |||
104 | /** |
||
105 | * Check if the given item exists on collection. |
||
106 | * |
||
107 | * @param string $name |
||
108 | * |
||
109 | * @return bool |
||
110 | */ |
||
111 | public function has($name) |
||
115 | |||
116 | /** |
||
117 | * Find a specific collection item or throw's an exception. |
||
118 | * |
||
119 | * @param string $name |
||
120 | * |
||
121 | * @return mixed |
||
122 | */ |
||
123 | public function findOrFail($name) |
||
133 | } |
||
134 |
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.