1 | <?php |
||
17 | class DeepCopy |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $hashMap = []; |
||
23 | |||
24 | /** |
||
25 | * Filters to apply. |
||
26 | * @var array |
||
27 | */ |
||
28 | private $filters = []; |
||
29 | |||
30 | /** |
||
31 | * Type Filters to apply. |
||
32 | * @var array |
||
33 | */ |
||
34 | private $typeFilters = []; |
||
35 | |||
36 | private $skipUncloneable = false; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | private $useCloneMethod; |
||
42 | |||
43 | /** |
||
44 | * @param bool $useCloneMethod If set to true, when an object implements the __clone() function, it will be used |
||
45 | * instead of the regular deep cloning. |
||
46 | */ |
||
47 | public function __construct($useCloneMethod = false) |
||
53 | |||
54 | /** |
||
55 | * Cloning uncloneable properties won't throw exception. |
||
56 | * @param $skipUncloneable |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function skipUncloneable($skipUncloneable = true) |
||
64 | |||
65 | /** |
||
66 | * Perform a deep copy of the object. |
||
67 | * @param mixed $object |
||
68 | * @return mixed |
||
69 | */ |
||
70 | public function copy($object) |
||
76 | |||
77 | public function addFilter(Filter $filter, Matcher $matcher) |
||
84 | |||
85 | public function addTypeFilter(TypeFilter $filter, TypeMatcher $matcher) |
||
92 | |||
93 | |||
94 | private function recursiveCopy($var) |
||
116 | |||
117 | /** |
||
118 | * Copy an array |
||
119 | * @param array $array |
||
120 | * @return array |
||
121 | */ |
||
122 | private function copyArray(array $array) |
||
130 | |||
131 | /** |
||
132 | * Copy an object |
||
133 | * @param object $object |
||
134 | * @return object |
||
135 | */ |
||
136 | private function copyObject($object) |
||
173 | |||
174 | private function copyObjectProperty($object, ReflectionProperty $property) |
||
207 | |||
208 | /** |
||
209 | * Returns first filter that matches variable, NULL if no such filter found. |
||
210 | * @param array $filterRecords Associative array with 2 members: 'filter' with value of type {@see TypeFilter} and |
||
211 | * 'matcher' with value of type {@see TypeMatcher} |
||
212 | * @param mixed $var |
||
213 | * @return TypeFilter|null |
||
214 | */ |
||
215 | private function getFirstMatchedTypeFilter(array $filterRecords, $var) |
||
229 | |||
230 | /** |
||
231 | * Returns first element that matches predicate, NULL if no such element found. |
||
232 | * @param array $elements |
||
233 | * @param callable $predicate Predicate arguments are: element. |
||
234 | * @return mixed|null |
||
235 | */ |
||
236 | private function first(array $elements, callable $predicate) |
||
246 | } |
||
247 |
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.