PHP Multi-Sample Mode introduction _php Tutorial

Source: Internet
Author: User
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 ...

  • Related Article

    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.