When learning java, you will know that there are multiple examples in the design mode. when learning java, you will know that there are multiple examples in the design mode:
1. Multiple instance types can have multiple instances
2. you must be able to create and manage your own instances and provide your own instances to the outside world.
We all know the PHP Singleton mode, but seldom talk about the PHP Singleton mode. Below is an example of the PHP Singleton mode seen on wikipedia:
The 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', $ string2 = 'darling '){
Echo "Goodbye $ string $ string2 \ n ";
}
}
$ A = Hello: getInstance ('world ');
$ B = Hello: getInstance ('Bob ');
// $! ==$ B
$ C = Hello: getInstance ('world ');
// $ A ===$ c
$ D = GoodBye: getInstance ();
$ E = GoodBye: getInstance ();
// $ D ===$ e
$ F = GoodBye: getInstance ('your ');
// $ D! ==$ F
?>
We can see that the key value needs to be transferred by getInstance () in the PHP multi-sample mode. for a given key value, the PHP multi-sample mode only has a unique object instance, and the PHP multi-sample mode saves memory, make sure that multiple instances of the same object do not conflict.