Mecha CMS

Updates.

Instance Counter

Author: Taufik Nurrohman · 74 Views

Tweet

Counting the class instances.

Table of Content

I have just added $__instance__ property to the Genome class, which will automatically store the class instances.

Before 

class Foo {}

$foo_1 = new Foo();
$foo_2 = new Foo();

// problem: how to count the number of `Foo`’s instance?

After 

class Foo extends Genome {}

$foo_1 = new Foo();
$foo_2 = new Foo();

echo count(Foo::$__instance__); // → 2

This internal ability can be used in the future, for example, to create singletones from dynamic classes like this:

class Bar extends Genome {
    public function __construct() {
        if (!self::$__instance__) {
            // trigger once!
        }
        parent::__construct();
    }
}

0000000000

0 Comments

No comments yet.