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