1 | <?php |
||
20 | class ObjectHelper |
||
21 | { |
||
22 | /** |
||
23 | * Configures an object with the initial property values. |
||
24 | * |
||
25 | * @param BaseObject $object |
||
26 | * @param array $properties |
||
27 | * @return BaseObject |
||
28 | */ |
||
29 | public static function populate(BaseObject $object, $properties = []): BaseObject |
||
40 | |||
41 | /** |
||
42 | * Create a new object |
||
43 | * |
||
44 | * @param $config |
||
45 | * @param string|null $instanceOf |
||
46 | * @throws InvalidConfigException |
||
47 | * @return BaseObject |
||
48 | */ |
||
49 | public static function create($config, string $instanceOf = null): BaseObject |
||
64 | |||
65 | /** |
||
66 | * Checks the config for a valid class |
||
67 | * |
||
68 | * @param $config |
||
69 | * @param string|null $instanceOf |
||
70 | * @param bool $removeClass |
||
71 | * @throws InvalidConfigException |
||
72 | * @return string |
||
73 | */ |
||
74 | public static function checkConfig(&$config, string $instanceOf = null, bool $removeClass = true): string |
||
94 | |||
95 | /** |
||
96 | * Get a class from a config |
||
97 | * |
||
98 | * @param $config |
||
99 | * @param bool $removeClass |
||
100 | * @throws InvalidConfigException |
||
101 | * @return string |
||
102 | */ |
||
103 | public static function getClassFromConfig(&$config, bool $removeClass = false): string |
||
119 | |||
120 | /** |
||
121 | * Find a class from a config |
||
122 | * |
||
123 | * @param $config |
||
124 | * @param bool $removeClass |
||
125 | * @return null|string |
||
126 | */ |
||
127 | public static function findClassFromConfig(&$config, bool $removeClass = false) |
||
159 | } |
||
160 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: