Learning Java will tell you that there are multiple patterns in the design pattern:
1, more than one instance class can have multiple instances
2. Multiple classes must be able to create and manage their own instances, and provide their own instances to the outside world.
We all know the PHP single example mode, but rarely say PHP multiple example mode, the following is seen in Wikipedia, see the example of PHP multiple examples:
Copy Code code as follows:
<?php
Abstract class Multiton {
private static $instances = Array ();
public static function getinstance () {
$key = Get_called_class (). Serialize (Func_get_args ());
if (!isset (self:: $instances [$key])) {
$RC = new Reflectionclass (Get_called_class ());
Self:: $instances [$key] = $RC->newinstanceargs (Func_get_args ());
}
Return self:: $instances [$key];
}
}
Class Hello extends Multiton {
Public function __construct ($string = ' world ') {
echo "Hello $string \ n";
}
}
Class GoodBye extends Multiton {
Public function __construct ($string = ' i ', $string 2 = ' darling ') {
echo "Goodbye $string $string 2\n";
}
}
$a = hello::getinstance (' World ');
$b = Hello::getinstance (' Bob ');
$a!== $b
$c = Hello::getinstance (' World ');
$a = = = $c
$d = Goodbye::getinstance ();
$e = Goodbye::getinstance ();
$d = = = $e
$f = goodbye::getinstance (' your ');
$d!== $f
?>
You can see that the PHP multiple example mode requires getinstance () to pass the key value, for a given key value, the PHP multi-example mode will only exist unique object instance, PHP Multiple example mode saves memory and ensures that multiple instances of the same object do not collide.