As I was browsing through Composer packages on Packagist I came across the EventDispatcher component from the Symfony framework, which can be used independent of Symfony (I believe all components of Symfony are now modular).
The EventDispatcher component allows you to adopt the observer pattern – events/listeners, events/observers (Magento and others), actions/hooks (WordPress), etc. They all follow the same basic pattern but have different implementations.
Once you run
composer require symfony/event-dispatcher in your project’s directory you’ll be ready to try it out. If you’re not familiar with Composer, get acquainted now. I don’t think we’d be having a PHP revolution without it.
I’ve put together a simple but complete example of how it can be used in the following Gist:
Note: Adjust the path to
autoload.php as needed.