Created
August 8, 2025 07:29
-
-
Save DianaSchef/42633fef546d42a0edde45473323c6d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // This code is what the Symfony Dependency Injection container performs | |
| // under the hood when registering a listener. | |
| $eventDispatcher->addListener( | |
| KernelEvents::REQUEST, | |
| function ($event) { | |
| // only when here, MyListener and ExpensiveDependency trigger autoloading | |
| // and the time of calling their constructor is spent. | |
| (new MyListener(new ExpensiveDependency))->onKernelRequest($event); | |
| }, | |
| ); | |
| // dispatch "unwraps" the listener by calling the closure, only then MyListener | |
| // and the ExpensiveDependency are created. | |
| $eventDispatcher->dispatch( | |
| new RequestEvent(/** ... */), KernelEvents::REQUEST) | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment