如何在PHP中的现有类中添加方法?

Gal*_*Gal 18 php inheritance class extend

我正在使用WordPress作为CMS,我想扩展其中一个类,而不必继承另一个类; 即我只想在该类中"添加"更多方法:

class A {

    function do_a() {
       echo 'a';
    }
}
Run Code Online (Sandbox Code Playgroud)

然后:

function insert_this_function_into_class_A() {
    echo 'b';
}
Run Code Online (Sandbox Code Playgroud)

(将后者插入A类的某种方式)

和:

A::insert_this_function_into_class_A();  # b
Run Code Online (Sandbox Code Playgroud)

这在顽固的PHP中甚至可能吗?

Gor*_*don 23

如果您只需要访问该类的Public API,则可以使用Decorator:

class SomeClassDecorator
{
    protected $_instance;

    public function myMethod() {
        return strtoupper( $this->_instance->someMethod() );
    }

    public function __construct(SomeClass $instance) {
        $this->_instance = $instance;
    }

    public function __call($method, $args) {
        return call_user_func_array(array($this->_instance, $method), $args);
    }

    public function __get($key) {
        return $this->_instance->$key;
    }

    public function __set($key, $val) {
        return $this->_instance->$key = $val;
    }

    // can implement additional (magic) methods here ...
}
Run Code Online (Sandbox Code Playgroud)

然后包装SomeClass的实例:

$decorator = new SomeClassDecorator(new SomeClass);

$decorator->foo = 'bar';       // sets $foo in SomeClass instance
echo $decorator->foo;          // returns 'bar'
echo $decorator->someMethod(); // forwards call to SomeClass instance
echo $decorator->myMethod();   // calls my custom methods in Decorator
Run Code Online (Sandbox Code Playgroud)

如果您需要访问protectedAPI,则必须使用继承.如果需要访问privateAPI,则必须修改类文件.虽然继承方法很好,但修改类文件可能会让您在更新时遇到麻烦(您将丢失所有补丁).但两者都比使用runkit更可行.

  • 大!.这不仅适用于向类添加新方法,还适用于在"extends"不是选项时覆盖它们的方法. (2认同)

小智 8

2014年的更新方式,可以应对范围.

public function __call($method, $arguments) {
    return call_user_func_array(Closure::bind($this->$method, $this, get_called_class()), $arguments);
}
Run Code Online (Sandbox Code Playgroud)

例如:

class stdObject {
    public function __call($method, $arguments) {
        return call_user_func_array(Closure::bind($this->$method, $this, get_called_class()), $arguments);
    }
}

$obj = new stdObject();
$obj->test = function() {
    echo "<pre>" . print_r($this, true) . "</pre>";
};
$obj->test();
Run Code Online (Sandbox Code Playgroud)

  • 这对于任何类名都非常有效。在$ obj-&gt; test中,您还可以执行$ this-&gt; someClassMethod()和$ this-&gt; privateVarName。您还可以在成员函数中创建可以访问类方法和变量的函数。 (2认同)