Mecha CMS

Deep learning on using the API.

Genome

Table of Content

Class extension.

2.0.02.2.0abstract

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 to another class:

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

Plug 

Add a method.

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

Example:

Add a birth method to the Blastula class:

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

Usage:

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

In object context:

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

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

Usage:

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

It is also possible to omit parentheses if method arguments are not primary. (there are no method arguments or there are default values for each method argument):

$foo = new Foo;
echo $foo->bar->baz->bar->x;

Same function, different default arguments:

function birth($name = 'Mecha', $entity = 'world') {
    return 'Hello ' . $entity . '! My name is ' . $name . '.';
}

/* Using the default method. */
Blastula::_('birth_1', 'birth');

/* Alter the first default argument value to `Mocha`. */
Blastula::_('birth_2', ['birth', ['Mocha']]);

/* Alter the first default argument value to `Mocha`. */
/* Limit arguments to `1` (changing the `$entity` parameter won’t give any effect here). */
Blastula::_('birth_3', ['birth', ['Mocha'], 1]);

Usage:

echo Blastula::birth_1(); // `Hello world! My name is Mecha.`
echo Blastula::birth_1('Mocha'); // `Hello world! My name is Mocha.`
echo Blastula::birth_1('Mocha', 'space'); // `Hello space! My name is Mocha.`

echo Blastula::birth_2(); // `Hello world! My name is Mocha.`
echo Blastula::birth_2('Machi'); // `Hello world! My name is Machi.`
echo Blastula::birth_2('Machi', 'space'); // `Hello space! My name is Machi.`

echo Blastula::birth_3(); // `Hello world! My name is Mocha.`
echo Blastula::birth_3('Machi'); // `Hello world! My name is Machi.`
echo Blastula::birth_3('Machi', 'space'); // `Hello world! My name is Machi.`

Kin 

Get the available methods.

static::_($kin);

Example:

$A = Blastula::_();
$B = Blastula::_('birth');
$C = Blastula::_('test'); // check for native method?

Result:

$A = [
    'birth' => ['birth']
];

$B = ['birth'];

$C = false;

Eject 

Remove a method.

static::_($kin, null);

Example:

Remove birth method from the Blastula class:

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