Mecha CMS

Deep learning on using the API.

Class Genome

Author: Taufik Nurrohman · 69 Views

Tweet

Dynamic methods.

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::plug($kin, $fn);

Example:

Add a birth method to the Blastula class:

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

Usage:

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

Usage in object context:

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

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

Usage:

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

Eject 

Remove a method.

Genome::eject($kin);

Example:

Remove birth method from the Blastula class:

Blastula::eject('birth');

Remove all custom methods from the Blastula class:

Blastula::eject();

Kin 

Get the available methods.

Genome::kin($kin = null, $fail = false, $origin = false);

Example:

$test_1 = Blastula::kin();
$test_2 = Blastula::kin('birth');
$test_3 = Blastula::kin('test');
$test_4 = Blastula::kin('test', false, true); // also, search for native method(s)

Result:

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

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

$test_3 = false;

$test_4 = 1;

0 Comments

No comments yet.