1 | <?php |
||
2 | /** |
||
3 | * |
||
4 | */ |
||
5 | |||
6 | namespace Mvc5\Event; |
||
7 | |||
8 | use Mvc5\Signal; |
||
9 | |||
10 | use function is_string; |
||
11 | use function key; |
||
12 | |||
13 | use const Mvc5\EVENT; |
||
0 ignored issues
–
show
|
|||
14 | |||
15 | trait EventModel |
||
16 | { |
||
17 | /** |
||
18 | * |
||
19 | */ |
||
20 | use Model; |
||
21 | |||
22 | /** |
||
23 | * @return array |
||
24 | */ |
||
25 | 3 | protected function args() : array |
|
26 | { |
||
27 | return [ |
||
28 | 3 | EVENT => $this |
|
29 | ]; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param callable $callable |
||
34 | * @param array $args |
||
35 | * @param callable|null $callback |
||
36 | * @return mixed |
||
37 | * @throws \Throwable |
||
38 | */ |
||
39 | 24 | protected function signal(callable $callable, array $args = [], callable $callback = null) |
|
40 | { |
||
41 | 24 | return Signal::emit($callable, $args, $callback); |
|
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param callable $callable |
||
46 | * @param array $args |
||
47 | * @param callable|null $callback |
||
48 | * @return mixed |
||
49 | * @throws \Throwable |
||
50 | */ |
||
51 | 3 | function __invoke(callable $callable, array $args = [], callable $callback = null) |
|
52 | { |
||
53 | 3 | return $this->signal( |
|
54 | 3 | $callable, !$args ? $this->args() : (!is_string(key($args)) ? $args : $this->args() + $args), $callback |
|
55 | ); |
||
56 | } |
||
57 | } |
||
58 |
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: