Reputation: 2199
Is there a way in PHP that, every time I instantiate a new class — e.g. $o = new SomeClass()
— I can "intercept" that and actually return some other class?
Or, more specifically, I want to do something like $o = new WrappingClass(new SomeClass())
and wrap all the "actual" classes in something else.
I thought maybe something like hooking into composer's autoloader, but that's really just responsible for knowing which files contain which classes, and require
ing them automagically. I don't see a way to hook into class instantiation.
My suspicion is no, unless I'm using some kind of application container or factory pattern, and having all my classes instantiated that way. In my case, that's not possible, as it's a legacy code base with several package dependencies, some of which's classes I'd like to be able to wrap.
Thanks!
Upvotes: 0
Views: 47