dongpa2000 2009-08-17 15:50
浏览 49
已采纳

在PHP中为已经实例化的对象添加自定义函数?

What's the best way to do something like this in PHP?:

$a = new CustomClass();

$a->customFunction = function() {
    return 'Hello World';
}

echo $a->customFunction();

(The above code is not valid.)

  • 写回答

3条回答 默认 最新

  • dswm97353 2009-08-17 16:38
    关注

    Here is a simple and limited monkey-patch-like class for PHP. Methods added to the class instance must take the object reference ($this) as their first parameter, python-style. Also, constructs like parent and self won't work.

    OTOH, it allows you to patch any callback type into the class.

    class Monkey {
    
        private $_overload = "";
        private static $_static = "";
    
    
        public function addMethod($name, $callback) {
            $this->_overload[$name] = $callback;
        }
    
        public function __call($name, $arguments) {
            if(isset($this->_overload[$name])) {
                array_unshift($arguments, $this);
                return call_user_func_array($this->_overload[$name], $arguments);
                /* alternatively, if you prefer an argument array instead of an argument list (in the function)
                return call_user_func($this->_overload[$name], $this, $arguments);
                */
            } else {
                throw new Exception("No registered method called ".__CLASS__."::".$name);
            }
        }
    
        /* static method calling only works in PHP 5.3.0 and later */
        public static function addStaticMethod($name, $callback) {
            $this->_static[$name] = $callback;
        }
    
        public static function __callStatic($name, $arguments) {
            if(isset($this->_static[$name])) {
                return call_user_func($this->_static[$name], $arguments);
                /* alternatively, if you prefer an argument list instead of an argument array (in the function)
                return call_user_func_array($this->_static[$name], $arguments);
                */
            } else {
                throw new Exception("No registered method called ".__CLASS__."::".$name);
            }
        }
    
    }
    
    /* note, defined outside the class */
    function patch($this, $arg1, $arg2) {
        echo "Arguments $arg1 and $arg2
    ";
    }
    
    $m = new Monkey();
    $m->addMethod("patch", "patch");
    $m->patch("one", "two");
    
    /* any callback type works. This will apply `get_class_methods` to the $m object. Quite useless, but fun. */
    $m->addMethod("inspect", "get_class_methods");
    echo implode("
    ", $m->inspect())."
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog