1 | <?php |
||
19 | class LogSubscriber implements EventSubscriberInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var LoggerInterface |
||
23 | */ |
||
24 | private $logger; |
||
25 | |||
26 | /** |
||
27 | * Constructor |
||
28 | * |
||
29 | * @param LoggerInterface $logger |
||
30 | */ |
||
31 | public function __construct(LoggerInterface $logger) |
||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | public static function getSubscribedEvents() |
||
45 | |||
46 | /** |
||
47 | * Log the request and response if it exists |
||
48 | * |
||
49 | * @param AfterSendEvent $event |
||
50 | */ |
||
51 | public function onAfterSend(AfterSendEvent $event) |
||
74 | |||
75 | /** |
||
76 | * Log an api exception with the request |
||
77 | * |
||
78 | * @param ApiExceptionEvent $event |
||
79 | */ |
||
80 | public function onApiException(ApiExceptionEvent $event) |
||
95 | } |
||
96 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.