PHP Single example: only in the entire page cycle only

Source: Internet
Author: User
Since ASP.net is compiled, a singleton is always present in the lifecycle of the application, and it is truly possible to be unique in the application life cycle.

The existence of a single example pattern is more meaningful in some cases, such as BlogEngine's site configuration is a single case pattern, and its loading and saving code is quite classic and interested to see its source code.

Since ASP.net is compiled, a singleton is always present in the lifecycle of the application, and it is truly possible to be unique in the application life cycle.

The implementation of the PHP single example pattern is as follows:

Class stat{
static $instance = NULL;
static function getinstance () {
if (self:: $instance = = NULL) {
Self:: $instance = new Stat ();
}
Return self:: $instance;
}

Private Function __construct () {
}

Private Function __clone () {
}
function Sayhi () {
Return "The Class are saying hi to u";
}
}
Echo stat::getinstance ()->sayhi ();
Class stat{
static $instance = NULL;
static function getinstance () {
if (self:: $instance = = NULL) {
Self:: $instance = new Stat ();
}
Return self:: $instance;
}
Private Function __construct () {
}

Private Function __clone () {
}
function Sayhi () {
Return "The Class are saying hi to u";
}
}
Echo stat::getinstance ()->sayhi ();

However, PHP is an interpreted language, where the single example seems to see no practical use, once the entire page is executed, the variable disappears.

static function getinstance () {
if (self:: $instance = = NULL) {
Self:: $instance = new Stat (); Here as the page overload will be executed again
}
Return self:: $instance;
}
static function getinstance () {
if (self:: $instance = = NULL) {
Self:: $instance = new Stat (); Here as the page overload will be executed again
}
Return self:: $instance;
}

I am not in touch with PHP for a long time, the above is written only in C # and PHP two different language environment for the single case mode of understanding, perhaps this example is just to illustrate that the pattern can be implemented in many languages, but does not mean that in all kinds of language environment can really work.

The single example in asp.net is unique across the application domain. A single example in PHP is only within the entire page cycle



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.