Home  >  Article  >  Backend Development  >  php-single pattern

php-single pattern

WBOY
WBOYOriginal
2016-07-30 13:29:27853browse
<code>static private $_instance = NULL;
</code>

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>

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>

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.

Statement:
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