• Home
  • blog
  • RESTful Remote Object Proxies with ProxyManager

RESTful Remote Object Proxies with ProxyManager

The proxy pattern is another cool design pattern in software development. A proxy is a class working as an interface to another class or web service. For the sake of simplicity, we’ll refer to proxied classes as subjects throughout the rest of the article. A proxy usually implements the same interface as the subject, so it looks like we’re calling the methods directly on the subject.

Although this article is not about the proxy pattern concept, we’ll discuss some basics, just to get started.

There are a variety of proxy types used for different purposes:

  1. Virtual Proxy – for lazily loading the resource-hungry objects, preventing them from occupying memory until they are needed.

  2. Protection Proxy – for limiting access to properties or methods of an object through a set of rules.

  3. Smart Reference – for adding additional behaviors when a method is called on the subject – suitable for aspect-oriented programming.

  4. Remote Objects – for accessing remote objects, hiding the fact that they are actually in a separate address space. This article will be mainly covering remote object proxies.

Let’s get started with a basic example of a virtual proxy:

class MyObjectProxy
    protected $subject;

    public function someMethod()

        return $this->subject->someMethod();

    private function init()
        if (null === $this->subject) {
            $this->subject = new Subject();

In the above code, we’re keeping a reference to the subject; each time a method is called against the proxy, init() is invoked, checking whether the subject has been instantiated yet, and if not, it will instantiate it.

Finally, the respective method on the subject is called:

// ...
return $this->subject->someMethod();
// ...

This is a very basic example of a value holder virtual proxy, which keeps a reference to the subject all the times. Although the previous example works just fine, this is not how we create proxies in this world. Instead, we’ll do what any other good developer would do: use a well-known, well-tested third-party library.

ProxyManager is a PHP library for creating various kinds of proxies through a set of factory classes.

Marco Pivetta, the author of ProxyManager, has a comprehensive presentation, which is an easy-to-understand introduction to the Proxy pattern. I also recommend taking a quick look at the official documentation of ProxyManager before we start.

Cutting to the Chase

This post is dedicated to one of the less-discussed proxy types known as remote object proxies. We will learn what they are and how they work. Then, we’ll move on to creating a remote object proxy, which is capable of interacting with a RESTful API.

Simply put, remote object proxies are used to interact with remote subjects (using HTTP as the transport mechanism) and disguise them as local objects!

Whenever a method is called against the proxy object, it tries to map the called method name to its remote counterpart. Then, it encodes and forwards the request to the remote method. All this happens in a matter of milliseconds.

We create our remote object proxies by using the RemoteObjectFactory class of ProxyManager. This factory requires an adapter to create a remote object proxy, capable of interacting with a certain remote subject. On the other hand, the adapter is responsible for transforming a request in a way that the remote subject can handle.

The adapters vary based on the implemented protocol by the remote subject; whether the subject is XML-RPC based, SOAP based or even a RESTful JSON API, the appropriate adapter should be used.

The code which follows will be using ProxyManager’s RemoteObjectFactory.

Currently, ProxyManager provides three adapters out of the box: XmlRpc, JsonRpc, and Soap. These adapters use an implementation of the ZendServerClient interface as a transporting mechanism.

To see examples of how it’s done, the official documentation is the best place.

In the next section, we will create what is lacking at the moment: a custom adapter suitable for RESTful APIs!

Continue reading %RESTful Remote Object Proxies with ProxyManager%