PHP multi-sample mode introduction _ PHP Tutorial

Source: Internet
Author: User
PHP multi-sample mode introduction. When learning java, you will know that there are multiple examples in the design mode: 1. multiple examples can have multiple instances. 2. multiple examples must be able to create and manage their own instances, and provide external users with their own learning java to 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.

Example 1. Multiple instance classes 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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.