Learning Java will tell you that there are multiple patterns in the design pattern:
1, multiple cases can have more than one instance
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 PHP singleton mode, but rarely say PHP multi-case mode, the following is the example of PHP in Wikipedia to see the multi-sample mode:
Copy CodeThe code is as follows:
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 = ' my ', $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 PHP multi-sample mode requires getinstance () to pass the key value, for a given key value, PHP multi-sample mode will only have a unique object instance, PHP Multi-sample mode to save memory, to ensure that multiple instances of the same object does not conflict.
http://www.bkjia.com/PHPjc/327868.html www.bkjia.com true http://www.bkjia.com/PHPjc/327868.html techarticle Learn Java to know that there are many patterns in the design pattern: 1, multiple instances of the class can have more than one instance 2, a number of classes must be able to self-create and manage their own instances, and to the outside world to provide their own real ...