Genome

Class extension.

This class acts as an extension for other classes so that they become capable of adding dynamic methods just like the prototype concept in JavaScript (if you know what I mean). This class works as a proxy to hold custom methods which are actually just closures stored in an internal static property which can then be exposed by calling it using the __call() and __callStatic() method. For example, we create a class named Test like this:

class Test extends Genome {

    public function internalMethod($value) {
        return 'internalMethod(' . z($value) . ')';
    }

    public static function internalMethodStatic($value) {
        return 'internalMethodStatic(' . z($value) . ')';
    }

}

Typically, you would call native methods like this on a PHP class instance:

echo (new Test)->internalMethod(1); // Returns “internalMethod(1)”
echo Test::internalMethodStatic(1); // Returns “internalMethodStatic(1)”

Since this class becomes a child of the Genome class, then you can add new methods directly in the same class without having to extend it to another class first:

Test::_('externalMethod', function ($value) {
    return 'externalMethod(' . z($value) . ')';
});

Test::_('externalMethodStatic', static function ($value) {
    return 'externalMethodStatic(' . z($value) . ')';
});

You can call these methods as “virtual methods” because they are dynamically created and called using overloading techniques.

echo (new Test)->externalMethod(1); // Returns “externalMethod(1)”
echo Test::externalMethodStatic(1); // Returns “externalMethodStatic(1)”

The difference between externalMethod() and externalMethodStatic() in the example above is that you will have $this context in method externalMethod() but not in method externalMethodStatic(). There is no difference in the way methods are created. The static keyword before the function declaration is optional for methods that will be called statically.