1 | <?php |
||
32 | trait InstanceFactoryTrait |
||
33 | { |
||
34 | /** |
||
35 | * instances pool |
||
36 | * |
||
37 | * @var object[] |
||
38 | * @access protected |
||
39 | */ |
||
40 | protected $pool = []; |
||
41 | |||
42 | /** |
||
43 | * for loop detection |
||
44 | * |
||
45 | * @var array |
||
46 | * @access protected |
||
47 | */ |
||
48 | protected $loop = []; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | * @access protected |
||
53 | */ |
||
54 | protected $counter = 0; |
||
55 | |||
56 | /** |
||
57 | * Get the instance either from the pool or create it |
||
58 | * |
||
59 | * @param string $id service id with or without the scope |
||
60 | * @param array $args arguments for the constructor |
||
61 | * @return object |
||
62 | * @throws LogicException if instantiation goes wrong |
||
63 | * @throws RuntimeException if method execution goes wrong |
||
64 | * @access protected |
||
65 | */ |
||
66 | protected function getInstance(/*# string */ $id, array $args) |
||
86 | |||
87 | /** |
||
88 | * Full scope info with consideration of ancestor instances |
||
89 | * |
||
90 | * @param string $id |
||
91 | * @return array |
||
92 | * @access protected |
||
93 | */ |
||
94 | protected function realScopeInfo(/*# string */ $id)/*# : array */ |
||
105 | |||
106 | /** |
||
107 | * Create the instance with loop detection |
||
108 | * |
||
109 | * Loop: an instance depends on itself in the creation chain. |
||
110 | * |
||
111 | * @param string $rawId |
||
112 | * @param array $args arguments for the constructor if any |
||
113 | * @return object |
||
114 | * @throws LogicException if instantiation goes wrong or loop detected |
||
115 | * @access protected |
||
116 | */ |
||
117 | protected function createInstance(/*# string */ $rawId, array $args) |
||
134 | } |
||
135 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.