1 | <?php |
||
7 | trait ToArray |
||
8 | { |
||
9 | private static $__depth; |
||
10 | |||
11 | private static $__maxDepth; |
||
12 | |||
13 | /** |
||
14 | * Reset depth and convert object to array. |
||
15 | * |
||
16 | * @param $subject |
||
17 | * @param int $maxDepth |
||
18 | * @return array|null |
||
19 | */ |
||
20 | 1 | public function __toArray($subject, $maxDepth = 3) |
|
28 | |||
29 | /** |
||
30 | * Convert object to array. |
||
31 | * |
||
32 | * @param $subject |
||
33 | * @return array|null |
||
34 | */ |
||
35 | 1 | public function ___toArray($subject) |
|
41 | |||
42 | /** |
||
43 | * Generate a callback to transform object to array. |
||
44 | * |
||
45 | * @return \Closure |
||
46 | */ |
||
47 | 1 | public function __getToArrayCallBack() |
|
76 | } |
||
77 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: