1 | <?php declare(strict_types = 1); |
||
16 | final class ErrorHandler implements ErrorHandlerContract |
||
17 | { |
||
18 | /** |
||
19 | * Controls whether error handler can throw exceptions. |
||
20 | * E.g. shutdown handler cannot produce new exceptions. |
||
21 | * |
||
22 | * @var bool |
||
23 | */ |
||
24 | private $canThrowException = true; |
||
25 | |||
26 | /** |
||
27 | * The error renderer. |
||
28 | * |
||
29 | * @var ErrorRenderer |
||
30 | */ |
||
31 | private $renderer; |
||
32 | |||
33 | /** |
||
34 | * The error reporter pipeline. |
||
35 | * |
||
36 | * @var ErrorReporterStack |
||
37 | */ |
||
38 | private $reporters; |
||
39 | |||
40 | /** |
||
41 | * ErrorHandler constructor. |
||
42 | * |
||
43 | * @param ErrorRenderer $renderer |
||
44 | * @param ErrorReporterStack $reporters |
||
45 | */ |
||
46 | 1 | public function __construct(ErrorRenderer $renderer, ErrorReporterStack $reporters) |
|
51 | |||
52 | /** |
||
53 | * @inheritdoc |
||
54 | * @throws ErrorException |
||
55 | */ |
||
56 | public function handleError(int $severity, string $message, string $filename, int $line): bool |
||
72 | |||
73 | /** |
||
74 | * {@inheritDoc} |
||
75 | */ |
||
76 | public function handleShutdown() |
||
85 | |||
86 | /** |
||
87 | * {@inheritDoc} |
||
88 | */ |
||
89 | public function handleThrowable(Throwable $e) |
||
97 | |||
98 | /** |
||
99 | * Checks if the error is severe enough and must be handled. |
||
100 | * |
||
101 | * @param int $severity |
||
102 | * @return bool |
||
103 | */ |
||
104 | private function isSevereEnough(int $severity): bool |
||
108 | |||
109 | } |
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: