Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm trying to implement dynamic proxies in PHP and I'm running into a problem of implementing interfaces.

My current approach to building a dynamic proxy is by having a class which uses the __call magic method. The issue is that this class implements no interface and/or extends no other class. So if I have a method which accepts only objects of a certain type through PHP type hints, I cannot pass an object of my dynamic proxy type.

One case I'm currently dealing with is a LazyLoader class, which checks the method being called to see if it should perform the loading procedure. My data source method for loading a user, instead of returning a User object, returns a LazyLoader object, which will load the user's friend list lazily. Many other data source methods will do similar things. The issue is that, there, I return a LazyLoader object and not a User object. So whenever I use a data source object to load a User object, I'll get back a LazyLoader object, which I can't pass to methods/functions which take a type hinted User parameter.

Any way to dynamically make a LazyLoader object "be a" User?

Edit: I could make several kinds of lazy loaders which extend the right class. For example, I could have a UserLazyLoader, a FinanceSheetLazyLoader and so forth. This is a possibility, but I'd like to see if I could solve this issue in a more generic way first.

share|improve this question
    
Using magic methods means you have a broken architecture, if it's a business logic –  bad_boy 2 days ago
    
@bad_boy The LazyLoader has the magic method. It (the class) contains no business logic. It only knows how to do lazy loading. The issue is that the way LazyLoader works is by "impersonating" a (for example) User object, but I can't (because of type hinting) use it where a User object is required. –  Pedro Henrique A. Oliveira 2 days ago
    
Why you can't type hint an interface instead of a class? So that you can pass any class that implements UserInterace –  bad_boy 2 days ago
    
@bad_boy I could do that, yes, but then my LazyLoader would have to implement a BUNCH of interfaces (as many as there are domain types for which I want to apply lazy loading), which is also not possible given I'm using __call magic method. My current approach is having several LazyLoader classes for each domain type (UserLazyLoader, and the like), which extend the domain types, and add lazy loading capabilities. However, there is a lot of boiler plate in doing that. –  Pedro Henrique A. Oliveira 2 days ago
1  
IFAIK this can only be done by code generation. Generating the proxy class source files and import them into your application. –  Bart yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.