Class · Mecha CMS

Deep learning on using the API.

Genome

Author: Taufik Nurrohman · 463 Views

Tweet

Class extension.

Table of Content

This class is purposed to be used as an extension to other classes so that the extended classes will be able to create their own dynamic methods. This is an abstract class, so you have to extend this class with another class:

class Blastula extends Genome {
    public static function test() {} // native method
}

Plug 

Add a method.

Genome::_($kin, $fn);

Example:

Add a birth method to the Blastula class:

Blastula::_('birth', function($name) {
    return 'Hello world! My name is ' . $name . '.';
});

Usage:

echo Blastula::birth('Mocha'); // → `Hello world! My name is Mocha.`

In object context:

class Foo extends Genome {
    public $x = 0;
    public function bar($i) {
        $this->x += $i;
        return $this;
    }
}

Foo::_('baz', function($i) {
    $this->x += $i;
    return $this;
});

Usage:

$foo = new Foo;
echo $foo->bar(4)->baz(1)->bar(2)->x;

Kin 

Get the available methods.

Genome::_($kin);

Example:

$test_1 = Blastula::_();
$test_2 = Blastula::_('birth');
$test_3 = Blastula::_('test'); // check for native method?

Result:

$test_1 = [
    'birth' => function($name) { … }
];

$test_2 = function($name) { … };

$test_3 = false;

Eject 

Remove a method.

Genome::_($kin, null);

Example:

Remove birth method from the Blastula class:

Blastula::_('birth', null);