25

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:

class A {

    function do_a() {
       echo 'a';
    }
}

then:

function insert_this_function_into_class_A() {
    echo 'b';
}

(some way of inserting the latter into A class)

and:

A::insert_this_function_into_class_A();  # b

Is this even possible in tenacious PHP?

Artefacto
  • 96,375
  • 17
  • 202
  • 225
Gal
  • 23,122
  • 32
  • 97
  • 118

4 Answers4

28

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.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 2
    Great!. This works not only for adding new methods to a class, but also for overwrite them when 'extends' is not an option. – juanra Apr 29 '15 at 11:09
  • Hmm...This doesn't actually add a method to the original object like [swift extensions](https://docs.swift.org/swift-book/LanguageGuide/Extensions.html) (entirely different from any type of inheritance or php `extends`), but it is a design workaround that essentially does the trick. – ScottyBlades Oct 06 '19 at 04:10
10

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();
Chris
  • 435
  • 4
  • 11
  • 2
    This works really well for any class name. In $obj->test you can also do $this->someClassMethod() and $this->privateVarName. You can also create functions inside member functions that can access the class methods and variables. – Luke Wenke Nov 18 '16 at 10:32
4

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

See runkit_method_add.

Pang
  • 9,564
  • 146
  • 81
  • 122
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • ... while runkit is nothing but a toy. – johannes Jun 10 '10 at 05:53
  • 1
    @johannes It's sometimes useful when debugging. But that's the reason for my recommendation, which does not, however, prevent me from actually answering the question. – Artefacto Jun 10 '10 at 06:01
  • 1
    Notice the big, red "EXPERIMENTAL" warning on the manual page? Not recommedned for any production use... – selfawaresoup Jun 10 '10 at 06:25
  • 2
    @Techpriester What's your point? That the OP ought to consider using another strategy? I get it, that advice is included in the response. However, this is a **working solution**. I try to actually answer the question, advice notwithstanding. Besides, you assume too much. Maybe the OP isnt using it for production (he didn't say he is). Maybe he's taking the runkit as a starting point to write his own stable extension to do the same. Whatever. It's not my intent to anticipate all the possible scenarios; I try to circumscribe to the actual question and leave second guessing the OP as a side game. – Artefacto Jun 10 '10 at 06:34
  • Well yes, but just wanted to make it clear that you shouldn't build a critical application on top of it running in production – johannes Jun 12 '10 at 22:05
-3

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.

selfawaresoup
  • 15,473
  • 7
  • 36
  • 47