PHP Multi-Example Pattern introduction _php Skill

Source: Internet
Author: User

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.

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.