由于asp.net是编译型的,所以单例一直会存在于这个应用程序的生命周期里,真正可以做到这个实例在应用程序生命周期中的唯一性。
单例模式的存在在一些情况下是比较有意义的,如BlogEngine的站点配置就是采用的单例模式,而且它的载入和保存的代码是相当经典的,有兴趣可以看看它的源代码。
由于asp.net是编译型的,所以单例一直会存在于这个应用程序的生命周期里,真正可以做到这个实例在应用程序生命周期中的唯一性。
php的单例模式的实现大致如下:
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 is 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 is saying hi to u ";
}
}
echo Stat::getInstance()->sayHi();
但,PHP是一种解释型的语言,在这里用单例好像看不出实际的用处,一旦整个页面执行完,变量也就消失了。
static function getInstance(){
if(self::$instance == NULL){
self::$instance = new Stat(); //这里随着页面的重载会再次执行
}
return self::$instance;
}
static function getInstance(){
if(self::$instance == NULL){
self::$instance = new Stat(); //这里随着页面的重载会再次执行
}
return self::$instance;
}
我接触PHP时间不长,以上写出来的只是在C#与PHP二种不同语言环境下对单例模式的认知,也许这个例子只是为了说明模式可以用很多种语言来实现,但并不意味着在各种语言环境下都能真正起作用。
asp.net中的单例是在整个应用程序域唯一。PHP中的单例只在整个页面周期内唯一

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
