TL;DR: To dynamically load classes by namespaces in PHP, you need a Registry, and a way to parse the concrete files from a directory based on its namespace.
Whenever I’m working on a project that has an object-oriented code base and that uses Subscribers and Services, I often use a Registry
. This makes it easy to
- register the subscribers with the core application whenever the code runs,
- de-couple any
Service
classes so they can be tested or even run isolation, - and maintain the code base whenever something has to be added or taken away.
One challenge with this approach though, at least in PHP, is that I’ve found myself having to go back into the Registry and set a reference to a given Subscriber
whenever I want to add it (or remove it whenever I want to, you know, remove it).
Ideally, I want my registry to know where the subscribers are and how to set them up. This way, I can focus on working on the rest of the code.
Continue reading