Understanding the broken instancing metaphore

Why I need to use runkit

In an earlier post I explained the way I’m dressing up an adapter class for mocking trying to solve the problem created by the fact that the class has no real methods but to wrap the global functions but the __get and __call magic ones.
Aside for some WordPress-related considerations the adapter classes implement a build_facade static method that will add methods to the classes using runkit

    class adclasses_Functions{

     * Builds the façade for the class filling it with methods
     * @param  string $file_path An optional file path to read the commma separated list of functions to add from.
     * @return bool            True if the facade was successfully built, false otherwise.
    public static function build_facade($file_path = null)
        // to avoid re-adding the methods
        if (self::$built_facade) {

            return false;
        if (null === $file_path) {
            // resort to the default one
            $file_path = dirname(__FILE__) . '/../assets/functions.txt';

        try {
            $contents = file_get_contents($file_path);
            if (false === $contents) {

                return false;
        catch(Exception $e) {

            return false;
        $functions_to_facade = explode(',', $contents);
        $functions_to_facade = array_unique($functions_to_facade);

        foreach ($functions_to_facade as $function_name) {
            runkit_method_add('adclasses_Functions', $function_name, '', 'return null;');
        // set the built_facade to avoid re-adding methods
        self::$built_facade = true;

        return true;

The method does little and loads the functions either from a passed file or from a default one, /assets/functions.txt. The file looks like this (cut to limit space)


where it’s clear that the functions to dump come from a WordPress run.

The persistence of the façade

From my current understanding runkit will modify the class for the whole execution of the PHP program or, in more practical terms, until the global PHP namespace persists.
Trying to add the same method to the same class twice will throw an error

$exit1 = runkit_add_method('SomeClass','someMethod','','return null;');
$exit1 = runkit_add_method('SomeClass','someMethod','','return null;');

and, in the case above, the way I have solved the problem is to use a bool, $built_facade, as a static class property to know if the façade of methods was built already and avoid attempting its building again.

Object get pulled rather than cast

It may come as natural but I’ve read (web) some confusion about the argument: PHPUnit mocks and stubs are instances of a class. Sure modified to suit the test needs but nonetheless instances.
Being instances of a class they will define properties and methods the class defines as instance properties and metods at the moment they are instanced, right?
Following along this line the following test should pass

* A class with no methods in it
class Bar
 * The test class
class runkit_Test extends PHPUnit_Framework_TestCase
    public function testInstancesDoNotUpdateMethods()
        // the Bar class has no explicitly defined instance methods
        $this->assertCount(0, array_keys(get_class_methods('Bar')));
        // I instance a class that has no explicitly defined instance methods
        $bar = new Bar();
        // I expect the method someMethod not to be defined
        $has_someMethod = method_exists($bar, 'someMethod');
        // add someMethod to the class defined instance methods
        runkit_method_add('Bar', 'someMethod', '', 'return null;');
        // the class should now have 1 instance method
        $this->assertCount(1, array_keys(get_class_methods('Bar')));
        // the object I instanced before adding the method to the class should not have the method...
        $has_someMethod = method_exists($bar, 'someMethod');
        $this->assertFalse($has_someMethod, 'bar should not have someMethod!');

But the test will fail like this

There was 1 failure:

1) runkit_Test::testMethods
bar should not have someMethod!
Failed asserting that true is false.


Tests: 1, Assertions: 4, Failures: 1.

Objects will add newly defined methods

So, to my surprise, the instance bar, created before someMethod was added to the class, will be upgraded to the new and latest defined instance methods. My idea, and the often used metaphore, of a mould-like class to cast instances is shattered and dynamism comes into play. I’m not that knowledgeable into PHP and runkit inner workings to understand exactly why but it’s nice to know.

Objects will upgrade re-defined methods

Not unexpectedly bar will not only sport the latest method added but also the latest version of it: the assertions below will all pass (imagine them appended to the previous test code)

// returns null as it's implementation in runkit_method_add specifies
// modify the method
runkit_method_redefine('Bar', 'someMethod', '', 'return true;');
// the method should upgrade

Objects will lose the removed methods

Again without surprise the object will not have methods removed by runkit defined and the assertions below, appended to the previous tests, will all pass.

// remove the method
runkit_method_remove('Bar', 'someMethod');
// object should not have someMethod
$has_someMethod = method_exists($bar, 'someMethod');