API development Article 3: perfect Singleton mode of PHP design patterns

Source: Internet
Author: User
: This article mainly introduces the third part of API development: the perfect Singleton mode of PHP design patterns. if you are interested in PHP tutorials, refer to it. Let's talk about the Singleton mode today.

I used to develop java. when I used the Singleton mode, I first thought of using the hungry Chinese style. then I found that PHP has the following features: PHP does not support granting non-basic type values to class member variables during class definition. Such as expressions and new operations. That's why this is not enough. In turn, we want to ensure the atomicity of this Singleton mode and find that PHP does not have thread security problems like JAVA. Hey, are you saying PHP is okay? So OK, next we will try the PHP lazy singleton mode.

Let's not talk about it first. I will first use the Singleton mode code of my first version:

// Define private static variables. this method is: lazy singleton (only this method in PHP) private static $ instance = null; // method of private function _ construct () {}// provides the public method for obtaining the instance public static function getInstance () {if (! (Self: $ instance instanceof self) {self: $ instance = new self ();} return self: $ instance;} // private _ clone method, prohibit copying object private function _ clone (){}
OK. this code looks perfect. there are comments and formats. it's okay. But when I was using it, I found a problem.

My class A is in Singleton mode, and my class B inherits from Class A. Then I call the following method:

$a = A::getInstance();$b = B::getInstance();var_dump($a === $b);
The output result is: bool (true)
What does this output mean? That is to say, after B inherits from A, B is also changed to the Singleton mode, so A and B are only inherited and managed, and their objects should not be equal, now, the two objects are exactly the same.
$b = B::getInstance();
The obtained object is still the object of Class A. What is the problem?

The problem lies in self. the reference of self is determined when the class is defined. that is to say, if A inherits B, its reference of self still points to. To solve this problem, the static binding feature is introduced in PHP 5.3. In short, the static keyword is used to access static methods or variables. unlike self, static references are determined by the runtime. So we simply rewrite our code so that the singleton mode can be reused.

class C{    protected static $_instance = null;    protected function __construct(){    }    protected function __clone(){    }    public function getInstance(){        if (static::$_instance === null) {            static::$_instance = new static;        }        return static::$_instance;    } }class D extends C{    protected static $_instance = null;}$c = C::getInstance();$d = D::getInstance();var_dump($c === $d);
This is the time when the output will be: bool (false)

Then it can be achieved. as long as the Singleton mode is inherited, its subclass is also the Singleton mode. You can achieve perfect reuse without having to write so much repeated code every time in the Singleton mode. Note that the above method can only be used in PHP 5.3. for PHP of the previous version, write a getInstance () method for each singleton class.

The above introduces the third part of API development: the perfect Singleton mode of PHP design mode, including some content, and hope to help friends who are interested in PHP tutorials.

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.