How to add a method to an existing class in PHP?

后端 未结 5 1266
情深已故
情深已故 2020-12-05 04:58

I\'m using WordPress as a CMS, and I want to extend one of its classes without having to inherit from another class; i.e. I simply want to \"add\" more methods to that class

相关标签:
5条回答
  • 2020-12-05 05:29

    No you can't dynamically change a class during runtime in PHP.

    You can accomplish this by either extending the class using regular inheritance:

    class Fancy extends NotSoFancy
    {
        public function whatMakesItFancy() //can also be private/protected of course
        {
            //    
        }
    }
    

    Or you could edit the Wordpress source files.

    I'd prefer the inheritance way. It's a lot easier to handle in the long run.

    0 讨论(0)
  • 2020-12-05 05:30

    If the class in question implements __call magic, then it's possible, and quite easy. If you want to know how this works I suggest you read Extending objects with new methods at runtime.

    0 讨论(0)
  • 2020-12-05 05:35

    An updated way for 2014 that copes with scope.

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

    Eg:

    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();
    
    0 讨论(0)
  • 2020-12-05 05:44

    You can use the runkit extension for this, but you should really consider regular inheritance instead.

    See runkit_method_add.

    0 讨论(0)
  • 2020-12-05 05:50

    If you only need to access the Public API of the class, you can use a 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 ...
    }
    

    Then wrap the instance of 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
    

    If you need to have access to the protected API, you have to use inheritance. If you need to access the private API, you have to modify the class files. While the inheritance approach is fine, modifiying the class files might get you into trouble when updating (you will lose any patches made). But both is more feasible than using runkit.

    0 讨论(0)
提交回复
热议问题