PHP Mode Encyclopedia – Multiple Instance Mode

The multiple-instance pattern means that there are multiple instances of the same class, and the instances are the class itself. This class is called a multi-instance class. The features of multi-instance mode are:

1.A multi-instance class can have multiple instances.
2.Multi-instance classes must create and manage their own instances by themselves, and provide their own instances to the outside world.

code example

class Container
{
private static $instances = [];
private function __construct()
{
}
public static function getInstance(string $name)
{
    if (! isset(self::$instances[$name])) {
         self::$instances[$name] = new self();
    }
    return self::$instances[$name];
}
private function __clone()
{
}
private function __wakeup()
{
}
}

Posted in: PHP

Leave a Reply

Your email address will not be published.

en_USEnglish