php-single pattern

WBOY
Release: 2016-07-30 13:29:27
Original
824 people have browsed it
<code>static private $_instance = NULL;
</code>
Copy after login

When the instance of the class does not exist, this method will create an instance of the class and return this instance. Normally, the name of this method is getInstance

<code>public function getInstance()
{
    if (self::$_instance == NULL) {
        self::$_instance = new SingleTon();
    }
    return self::$_instance;
}
</code>
Copy after login

If the user tries to use new or _clone to create a new object of a class, it will break the restrictions of the singleton mode. Therefore, it is also necessary to declare these two methods as private.

<code>private function __construct(){
}
private function __clone(){
}
</code>
Copy after login

Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.

The above has introduced the php-single pattern, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!