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);
}
}
请先登录再写评论。
Hi there,
I'm not 100% sure what you mean by "proxy methods" here.
In any case:
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
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.
Are there news about this feature? Is there a code template for create this feature with common Phpstorm tools?
I my opinion is not necessary that Bar implements FooInterface to have a proxy method to the echoStuff method of inner Foo object.
@Danilo Sanchi
Nothing yet AFAIK (unless some existing custom plugin can help here)
Original ticket: https://youtrack.jetbrains.com/issue/WI-17839