Feature Request - Auto generate proxy methods

In our system we favor composition over inheritence, it would be very useful if there was a code generator that would automatically create the proxy methods of the composed objects.

Example


interface FooInterface {
    public function echoStuff($stuff);
}



Class Foo implements FooInterface {
    public function echoStuff($stuff) {
        echo $stuff;
    |
}

Class Bar implements FooInterface {

    /**
     * @var Foo - Perhaps right click this property and have an option in the context menu to generate proxy methods
     */
    private $foo;

    public function __construct(Foo $foo) {
        $this->foo = $foo;
    }

    /** This function was auto-generated by the awesomeness of PHPStorm */
    public function echoStuff($stuff) {
        $this->foo->echoStuff($stuff);
    }
}
3 comments

Hi there,

I'm not 100% sure what you mean by "proxy methods" here.

In any case:

  1. You can generate getter/setter via universal Alt+Enter quick-fix menu: place cursor is on $foo in private $foo; line and choose appropriate entry from menu (or "Code | Generate...")
  2. You can override any method of the parent class via "Code | Override Methods..." or "Code | Implement Methods..." for interface/abstract methods while standing inside the target class.
0

A proxy method is simply a method in a composed class that calls a method of a containing class. In the case above, the Foo class is the true implementation of the echoStuff method and inside the Bar class we simply "proxy" it by creating a method with the same signature and calling $this->foo-echoStuff($stuff) from within it.

Using the current auto-generators will not work because, we are neither implementing setters/getters nor wanting to redefine the methods of the interface.
Using composition also provides a way of accomplishing multiple inheritance using interfaces. To further elaborate the Bar class could also take a Baz object as a constructor parameter and implement the Baz interface. It would now be a type of both Foo, and a Bazz. However the role of Bar isn't to 'implement' the methods but rather to proxy them. These use cases become quite common when you are using Dependency Injection.

Also a few other terms for "proxy method" are "forwarding method" or "wrapper method"
http://en.wikipedia.org/wiki/Wrapper_function

Other References:
http://en.wikipedia.org/wiki/Composition_over_inheritance

0

I see. Thanks.

All currently available "code generators" are located under "Code | Generate..." -- AFAIK it has nothing that can do exactly what you require.

Please file a Feature Request ticket to the Issue Tracker and hopefully it will be implemented in one of the future versions.

0

Please sign in to leave a comment.