UKWD
UKWD

Reputation: 75

PHP proxy objects

Am I right in saying proxy objects are like an alias of another object / entity? How are proxy objects used? Could anyone give a basic example?

Any replies are hugely appreciated.

Upvotes: 2

Views: 4323

Answers (1)

Marino Di Clemente
Marino Di Clemente

Reputation: 3190

From GOF - Design Patterns book:

Proxy object provide a surrogate or placeholder for another object to control access to it.

So a class A(proxy) can extend the B(RealObject) class or implement its interfaces. Usually proxy do some intermediate steps between the callers and the real object without the callers knowing they were facing a proxy.

There are many more specific patterns that results from proxy concept: Decorator (add more features), Remote Proxy (hide comunication between objects), Protection Proxy (control over comunication between objects), Lazy Instantiation (postpone the loading of grow objects only when they are needed) and others. All these Patterns don't add extra methods to the real object but add some features to the existing methods.

I suggest you to read Giorgio Sironi example in Practical php patterns book.

For a real example Doctrine uses proxy for lazy loading of related objects.

Upvotes: 7

Related Questions