PHP:使用__call覆盖父方法 [英] PHP: Overriding parent methods with __call

查看:280
本文介绍了PHP:使用__call覆盖父方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以某种方式隐藏父方法,以便为父类上定义的方法调用子类的__call魔术方法.例如:

I'd like to hide parent methods in some manner so that a child class's __call magic method is invoked for methods defined on the parent. For example:

class C {
  function foo() { echo "foo\n"; }
}

class D extends C {
  function __call( $method, $args ) { echo "called $name\n"; }
}

$d = new D();
$d->foo();

// desired result: called foo
// actual result: foo

我已经签入了rename_function和override_function,但是这些方法不适用于方法. ReflectionMethod有一个我尝试过的setAccessible方法,但这似乎仅在您使用ReflectionMethod实例调用该方法时才起作用.

I've checked into rename_function and override_function but those don't work for methods. ReflectionMethod has a setAccessible method I tried, but that seems to only work if you use the ReflectionMethod instance to invoke the method.

背景:尝试拥有一个可以利用PHP类型检查的RPC Proxy类.因此,如果我有RPCFoo,则可以使用类型提示或instanceof强制类型,以便可以检查($ RPCFoo实例Foo).

Background: attempting to have an RPC Proxy class that can take advantage of PHP's type checking. So, if I have an RPCFoo I can use type hinting or instanceof to enforce type in such a way that I can check that ($RPCFoo instance Foo).

基类应该可以直接使用,而无需使用代理.这样,代理可以是系统配置的一部分,而不是代码的一部分.考虑多个具有相同代码库的服务器,但是可以为每个服务器分配某些任务.如果本地服务器不处理请求的服务,则类加载器将返回代理而不是基类.如果本地服务器确实处理了请求的服务,它将返回基类.

the base class should be able to be used as is, without the proxy. This is so that proxying can be part of the system configuration, not the code. Think multiple servers all with the same code base, but the ability to assign certain tasks to each. If the local server does not handle the service requested, the class loader would return a proxy instead of the base class. If the local server does handle the service requested, it would return the base class.

再次所提供的方法是可行的,但对IDE和反射隐藏了基类的设计接口.我试图使代理实现变得干净,以便仅从基类继承并实现代理接口.为了维护基类接口,代理开发人员必须重新实现所有公共和受保护的方法才能进行远程调用.然后,当基类中的某些内容更新时,代理也必须更新.最后,我想我只是走编写使用反射的代理生成器的路线为开发人员做的.

Edit Again: the methods presented are workable, but hide the designed interface of the base class from IDEs and reflection. I was attempting to make the proxy implementation clean so that one simply had to inherit from the base class and implement a proxy interface. In order to maintain the base class interface, the proxy developer would have to re-implement all the public and protected methods to make a remote call. Then when something is updated in the base class, the proxy would have to be updated as well. In the end, I think I'm simply going to go the route of writing a proxy generator that uses reflection do this for the developer.

感谢您的答复!

推荐答案

您必须将对foo方法的访问权限设置为private:

You have to set access to foo method to private :

class C {
    private function foo() {
        echo "foo\n";
    }
    public function __call($method, $args) {
        if(is_callable(array($this,$method))) {
            return call_user_func_array(array($this,$method), $args);
        } else {
            trigger_error("Call to undefined method '{$method}'");
        }
    }
}

这篇关于PHP:使用__call覆盖父方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆