如题所述
ããåä¾æ¨¡å¼çè¦ç¹æä¸ä¸ªï¼
ããä¸æ¯æ个类åªè½æä¸ä¸ªå®ä¾ï¼
ããäºæ¯å®å¿ é¡»èªè¡å建è¿ä¸ªå®ä¾ï¼
ããä¸æ¯å®å¿ é¡»èªè¡åæ´ä¸ªç³»ç»æä¾è¿ä¸ªå®ä¾ã
ããå¤å¶ä»£ç 代ç å¦ä¸:
ãã<?php
ãã/* åä¾æ¨¡å¼ä¸¾ä¾,å ¶è¦ç¹å¦ä¸:
ãã*
ãã* 1. $_instance å¿ é¡»å£°æ为éæçç§æåé
ãã* 2. æé å½æ°åå éå½æ°å¿ 须声æ为ç§æç,è¿æ¯ä¸ºäºé²æ¢å¤é¨ç¨åº new ç±»ä»è失å»åä¾æ¨¡å¼çæä¹
ãã* 3. getInstance()æ¹æ³å¿ 须声æä¸ºå ¬æç,å¿ é¡»è°ç¨æ¤æ¹æ³ä»¥è¿åå¯ä¸å®ä¾çä¸ä¸ªå¼ç¨
ãã* 4. ::æä½ç¬¦åªè½è®¿é®éæåéæéæå½æ°
ãã* 5. PHPçåä¾æ¨¡å¼æ¯ç¸å¯¹èè¨ç,å 为PHPç解éè¿è¡æºå¶ä½¿å¾æ¯ä¸ªPHP页é¢è¢«è§£éæ§è¡åï¼ææçç¸å ³èµæºé½ä¼è¢«åæ¶ã
ãã* ä¹å°±æ¯è¯´ï¼PHPå¨è¯è¨çº§å«ä¸æ²¡æåæ³è®©æ个对象常驻å åãå¨PHPä¸ï¼ææçåéé½æ¯é¡µé¢çº§çï¼æ 论æ¯å ¨å±åéï¼
ãã* è¿æ¯ç±»çéææåï¼é½ä¼å¨é¡µé¢æ§è¡å®æ¯åè¢«æ¸ ç©º,ç»æä¼éæ°å»ºç«æ°ç对象ï¼è¿æ ·ä¹å°±å®å ¨å¤±å»äºSingletonçæä¹ã
ãã* ä¸è¿,å¨å®é åºç¨ä¸åä¸ä¸ªé¡µé¢ä¸å¯è½ä¼åå¨å¤ä¸ªä¸å¡é»è¾,è¿æ¶åä¾æ¨¡å¼å°±èµ·å°äºå¾éè¦çä½ç¨,ææçé¿å äºéå¤
ãã* new 对象(注: new 对象ä¼æ¶èå åèµæº)è¿ä¹ä¸ä¸ªè¡ä¸º,æ以æ们说PHPçåä¾æ¨¡å¼æ¯ç¸å¯¹èè¨ç
ãã*
ãã*/
ããclass People
ãã{
ããstatic private $_instance = NULL;
ããpublic $height = '';
ããpublic $age = '';
ããprivate function __construct()
ãã{
ãã$this->height = '185';
ãã$this->age = 25;
ãã}
ããprivate function __clone()
ãã{
ãã//do something
ãã}
ããstatic public function getInstance()
ãã{
ããif(!self::$_instance instanceof self)
ãã{
ãã//echo 'lgh-big';
ããself::$_instance = new self;
ãã}
ããelse
ãã{
ãã//for testing only
ãã//echo 'gdc-xiaoairener';
ãã}
ããreturn self::$_instance;
ãã}
ããpublic function getHeight()
ãã{
ããecho $this->height;
ãã}
ããpublic function getAge()
ãã{
ããecho $this->age;
ãã}
ãã}
ããfunction testInstance()
ãã{
ããPeople::getInstance()->getAge();
ãã}
ãã//begin to use the class
ãã$lgh = People::getInstance();
ãã$lgh->getHeight();
ããecho '<br />';
ããtestInstance();
ãã?>
ããä¼ç¹ï¼åä¾æ¨¡å¼å¯ä»¥é¿å 大éçnewæä½ï¼å 为æ¯ä¸æ¬¡newæä½é½ä¼æ¶èå åèµæºåç³»ç»èµæº
ãã缺ç¹ï¼å¨PHPä¸ï¼ææçåéæ 论æ¯å ¨å±åéè¿æ¯ç±»çéææåï¼é½æ¯ 页é¢çº§çï¼æ¯æ¬¡é¡µé¢è¢«æ§è¡æ¶ï¼é½ä¼éæ°å»ºç«æ°ç对象ï¼é½ä¼å¨é¡µé¢æ§è¡å®æ¯åè¢«æ¸ ç©ºï¼è¿æ ·ä¼¼ä¹PHPåä¾æ¨¡å¼å°±æ²¡æä»ä¹æä¹äºï¼æ以PHPåä¾æ¨¡å¼æè§å¾åª æ¯é对å次页é¢çº§è¯·æ±æ¶åºç°å¤ä¸ªåºç¨åºæ¯å¹¶éè¦å ±äº«åä¸å¯¹è±¡èµæºæ¶æ¯é常ææä¹çã
ããWhyâ为ä»ä¹è¦ä½¿ç¨PHPåä¾æ¨¡å¼ï¼
ããPHPçä¸ä¸ªä¸»è¦åºç¨åºåå°±æ¯åºç¨ç¨åºä¸æ°æ®åºæ交éçåºç¨åºæ¯ï¼æ以ä¸ä¸ªåºç¨ä¸ä¼åå¨å¤§éçæ°æ®åºæä½ï¼æ¯å¦è¿æ°æ®åºå¥ææ¥è¿æ¥æ°æ®åºè¿ä¸è¡ä¸ºï¼ä½¿ç¨åä¾æ¨¡å¼å¯ä»¥é¿å 大éçnewæä½ï¼å 为æ¯ä¸æ¬¡newæä½é½ä¼æ¶èå åèµæºåç³»ç»èµæºã
ããè¿æ¯æäºæ½è±¡ï¼ç»åºä»£ç ç段ã
ãã使ç¨ä¼ ç»æ¹å¼ç¼ç
ããå¤å¶ä»£ç 代ç å¦ä¸:
ãã<?php
ãã......
ãã//åå§åä¸ä¸ªæ°æ®åºå¥æ
ãã$db = new DB(...);
ãã//æ¯å¦æ个åºç¨åºæ¯æ¯æ·»å ä¸æ¡ç¨æ·ä¿¡æ¯ï¼
ãã$db->addUserInfo();
ãã......
ãã//ç¶èæ们å¨å¦å¤ä¸ä¸ªå°æ¹å¯è½è¦æ¥æ¾ç¨æ·çä¿¡æ¯ï¼è¿ä¸ªæ æ¯åºç°å¨ä¸ä¸ªå½æ°ä¸ï¼è¿æ¶è¦ç¨å°æ°æ®åºå¥æèµæºï¼æ们å¯è½éè¦è¿ä¹å»å
ãã......
ããfunction test(){
ãã......
ãã//è¿æ¶æ们ä¸å¾ä¸éæ°åå§åä¸ä¸ªæ°æ®åºå¥æ,è¯æ³å¤ä¸ªåºç¨åºæ¯ä¸ï¼è¿æ ·ç代ç æ¯å¤ä¹å¯æåï¼ï¼
ãã$db = new DB(...);
ãã$db->getUserInfo();
ãã......
ãã//æäºæåæ许ä¼è¯´ï¼æä¹å¯ä»¥ä¸è¿æ ·ååï¼æç´æ¥å©ç¨globalå ³é®åä¸å°±å¯ä»¥äºåï¼çç¡®ï¼globalå¯ä»¥è§£å³é®é¢ï¼ä¹èµ·å°äºåä¾æ¨¡å¼çä½ç¨ï¼ä½æ¯OOPä¸ï¼æ们æç»è¿æ ·æ¥ç¼å代ç ï¼å 为globalåå¨å®å ¨éæ£ï¼è¯·åèç¸å ³ä¹¦ç±ï¼åæ¶åä¾æ¨¡å¼æ°æ°æ¯å¯¹å ¨å±åéçä¸ç§æ¹è¿ï¼é¿å äºé£äºåå¨å¯ä¸å®ä¾çå ¨å±åé污æå½å空é´
ããglobal $db; //OOPä¸ï¼æ们ä¸æå¡è¿æ ·ç¼å代ç
ãã......
ãã}
ãã使ç¨åä¾æ¨¡å¼ç¼ç
ããå¤å¶ä»£ç 代ç å¦ä¸:
ãã<?php
ãã......
ãã//ææçåºç¨æ æ¯åªæä¸ä¸ªæ°æ®åºå¥æèµæºï¼å¿å¿ï¼æçèé«äºï¼
ãã//èµæºä¹å¤§å¤§çå¾å°èçï¼ä»£ç ç®æ´æäºï¼ï¼
ããDB::getInstance()->addUserInfo();
ããDB::getInstance()->getUserInfo();
ãã......
ããHowâå¦ä½æ¥ç¼åPHPåä¾æ¨¡å¼ï¼
ããå¨äºè§£äºåä¾æ¨¡å¼çåºç¨åºæ¯ä¹åï¼ä¸é¢æ们éè¿ç¼ååä¾æ¨¡å¼çå ·ä½å®ç°ä»£ç æ¥ææ¡PHPåä¾æ¨¡å¼çæ ¸å¿è¦ç¹ï¼ä»£ç å¦ä¸ï¼
ããå¤å¶ä»£ç 代ç å¦ä¸:
ãã<?php
ãã/**
ãã* PHPåä¾æ¨¡å¼æ¼ç¤ºä¸¾ä¾
ãã* @author guohua.li
ãã* @modify 2010-07-11
ãã* @website http://blog.163.com/lgh_2002/
ãã*/
ããclass User{
ãã/**
ãã* éææååé ä¿åå ¨å±å®ä¾
ãã* @access private
ãã*/
ããstatic private $_instance = NULL;
ãã/**
ãã* ç§æåæé å½æ°ï¼é²æ¢å¤çå®ä¾å对象
ãã*/
ããprivate function __construct() {}
ãã/**
ãã* ç§æåå éå½æ°ï¼é²æ¢å¤çå é对象
ãã*/
ããprivate function __clone(){}
ãã/**
ãã* éææ¹æ³, åä¾ç»ä¸è®¿é®å ¥å£
ãã* @return object è¿å对象çå¯ä¸å®ä¾
ãã*/
ããstatic public function getInstance() {
ããif (is_null(self::$_instance) || !isset(self::$_instance)) {
ããself::$_instance = new self();
ãã}
ããreturn self::$_instance;
ãã}
ãã/**
ãã* æµè¯æ¹æ³: è·åç¨æ·åå
ãã*/
ããpublic function getName() {
ããecho 'hello liguohua!';
ãã}
ãã}
ããä»ä»¥ä¸ä»£ç ä¸ï¼æ们æ»ç»åºPHPåä¾æ¨¡å¼å®ç°çæ ¸å¿è¦ç¹æå¦ä¸ä¸æ¡ï¼
ãã1.éè¦ä¸ä¸ªä¿åç±»çå¯ä¸å®ä¾çéææååéï¼é常为$_instanceç§æåéï¼
ãã2.æé å½æ°åå éå½æ°å¿ 须声æ为ç§æçï¼è¿æ¯ä¸ºäºé²æ¢å¤é¨ç¨åºnewç±»ä»è失å»åä¾æ¨¡å¼çæä¹
ãã3.å¿ é¡»æä¾ä¸ä¸ªè®¿é®è¿ä¸ªå®ä¾çå ¬å ±çéææ¹æ³ï¼é常为getInstanceæ¹æ³ï¼ï¼ä»èè¿åå¯ä¸å®ä¾çä¸ä¸ªå¼ç¨
ããPHPåä¾æ¨¡å¼ç缺ç¹
ããä¼æå¨ç¥ï¼PHPè¯è¨æ¯ä¸ç§è§£éåçèæ¬è¯è¨ï¼è¿ç§è¿è¡æºå¶ä½¿å¾æ¯ä¸ªPHP页é¢è¢«è§£éæ§è¡åï¼ææçç¸å ³èµæºé½ä¼è¢«åæ¶ãä¹å°±æ¯è¯´ï¼PHPå¨è¯è¨çº§å«ä¸æ²¡æåæ³è®©æ个对象常驻å åï¼è¿åasp.netãJavaçç¼è¯åæ¯ä¸åçï¼æ¯å¦å¨Javaä¸åä¾ä¼ä¸ç´åå¨äºæ´ä¸ªåºç¨ç¨åºççå½å¨æéï¼åéæ¯è·¨é¡µé¢çº§çï¼çæ£å¯ä»¥åå°è¿ä¸ªå®ä¾å¨åºç¨ç¨åºçå½å¨æä¸çå¯ä¸æ§ãç¶èå¨PHPä¸ï¼ææçåéæ 论æ¯å ¨å±åéè¿æ¯ç±»çéææåï¼é½æ¯é¡µé¢çº§çï¼æ¯æ¬¡é¡µé¢è¢«æ§è¡æ¶ï¼é½ä¼éæ°å»ºç«æ°ç对象ï¼é½ä¼å¨é¡µé¢æ§è¡å®æ¯åè¢«æ¸ ç©ºï¼è¿æ ·ä¼¼ä¹PHPåä¾æ¨¡å¼å°±æ²¡æä»ä¹æä¹äºï¼æ以PHPåä¾æ¨¡å¼æè§å¾åªæ¯é对å次页é¢çº§è¯·æ±æ¶åºç°å¤ä¸ªåºç¨åºæ¯å¹¶éè¦å ±äº«åä¸å¯¹è±¡èµæºæ¶æ¯é常ææä¹çã
ããä¸æ¯æ个类åªè½æä¸ä¸ªå®ä¾ï¼
ããäºæ¯å®å¿ é¡»èªè¡å建è¿ä¸ªå®ä¾ï¼
ããä¸æ¯å®å¿ é¡»èªè¡åæ´ä¸ªç³»ç»æä¾è¿ä¸ªå®ä¾ã
ããå¤å¶ä»£ç 代ç å¦ä¸:
ãã<?php
ãã/* åä¾æ¨¡å¼ä¸¾ä¾,å ¶è¦ç¹å¦ä¸:
ãã*
ãã* 1. $_instance å¿ é¡»å£°æ为éæçç§æåé
ãã* 2. æé å½æ°åå éå½æ°å¿ 须声æ为ç§æç,è¿æ¯ä¸ºäºé²æ¢å¤é¨ç¨åº new ç±»ä»è失å»åä¾æ¨¡å¼çæä¹
ãã* 3. getInstance()æ¹æ³å¿ 须声æä¸ºå ¬æç,å¿ é¡»è°ç¨æ¤æ¹æ³ä»¥è¿åå¯ä¸å®ä¾çä¸ä¸ªå¼ç¨
ãã* 4. ::æä½ç¬¦åªè½è®¿é®éæåéæéæå½æ°
ãã* 5. PHPçåä¾æ¨¡å¼æ¯ç¸å¯¹èè¨ç,å 为PHPç解éè¿è¡æºå¶ä½¿å¾æ¯ä¸ªPHP页é¢è¢«è§£éæ§è¡åï¼ææçç¸å ³èµæºé½ä¼è¢«åæ¶ã
ãã* ä¹å°±æ¯è¯´ï¼PHPå¨è¯è¨çº§å«ä¸æ²¡æåæ³è®©æ个对象常驻å åãå¨PHPä¸ï¼ææçåéé½æ¯é¡µé¢çº§çï¼æ 论æ¯å ¨å±åéï¼
ãã* è¿æ¯ç±»çéææåï¼é½ä¼å¨é¡µé¢æ§è¡å®æ¯åè¢«æ¸ ç©º,ç»æä¼éæ°å»ºç«æ°ç对象ï¼è¿æ ·ä¹å°±å®å ¨å¤±å»äºSingletonçæä¹ã
ãã* ä¸è¿,å¨å®é åºç¨ä¸åä¸ä¸ªé¡µé¢ä¸å¯è½ä¼åå¨å¤ä¸ªä¸å¡é»è¾,è¿æ¶åä¾æ¨¡å¼å°±èµ·å°äºå¾éè¦çä½ç¨,ææçé¿å äºéå¤
ãã* new 对象(注: new 对象ä¼æ¶èå åèµæº)è¿ä¹ä¸ä¸ªè¡ä¸º,æ以æ们说PHPçåä¾æ¨¡å¼æ¯ç¸å¯¹èè¨ç
ãã*
ãã*/
ããclass People
ãã{
ããstatic private $_instance = NULL;
ããpublic $height = '';
ããpublic $age = '';
ããprivate function __construct()
ãã{
ãã$this->height = '185';
ãã$this->age = 25;
ãã}
ããprivate function __clone()
ãã{
ãã//do something
ãã}
ããstatic public function getInstance()
ãã{
ããif(!self::$_instance instanceof self)
ãã{
ãã//echo 'lgh-big';
ããself::$_instance = new self;
ãã}
ããelse
ãã{
ãã//for testing only
ãã//echo 'gdc-xiaoairener';
ãã}
ããreturn self::$_instance;
ãã}
ããpublic function getHeight()
ãã{
ããecho $this->height;
ãã}
ããpublic function getAge()
ãã{
ããecho $this->age;
ãã}
ãã}
ããfunction testInstance()
ãã{
ããPeople::getInstance()->getAge();
ãã}
ãã//begin to use the class
ãã$lgh = People::getInstance();
ãã$lgh->getHeight();
ããecho '<br />';
ããtestInstance();
ãã?>
ããä¼ç¹ï¼åä¾æ¨¡å¼å¯ä»¥é¿å 大éçnewæä½ï¼å 为æ¯ä¸æ¬¡newæä½é½ä¼æ¶èå åèµæºåç³»ç»èµæº
ãã缺ç¹ï¼å¨PHPä¸ï¼ææçåéæ 论æ¯å ¨å±åéè¿æ¯ç±»çéææåï¼é½æ¯ 页é¢çº§çï¼æ¯æ¬¡é¡µé¢è¢«æ§è¡æ¶ï¼é½ä¼éæ°å»ºç«æ°ç对象ï¼é½ä¼å¨é¡µé¢æ§è¡å®æ¯åè¢«æ¸ ç©ºï¼è¿æ ·ä¼¼ä¹PHPåä¾æ¨¡å¼å°±æ²¡æä»ä¹æä¹äºï¼æ以PHPåä¾æ¨¡å¼æè§å¾åª æ¯é对å次页é¢çº§è¯·æ±æ¶åºç°å¤ä¸ªåºç¨åºæ¯å¹¶éè¦å ±äº«åä¸å¯¹è±¡èµæºæ¶æ¯é常ææä¹çã
ããWhyâ为ä»ä¹è¦ä½¿ç¨PHPåä¾æ¨¡å¼ï¼
ããPHPçä¸ä¸ªä¸»è¦åºç¨åºåå°±æ¯åºç¨ç¨åºä¸æ°æ®åºæ交éçåºç¨åºæ¯ï¼æ以ä¸ä¸ªåºç¨ä¸ä¼åå¨å¤§éçæ°æ®åºæä½ï¼æ¯å¦è¿æ°æ®åºå¥ææ¥è¿æ¥æ°æ®åºè¿ä¸è¡ä¸ºï¼ä½¿ç¨åä¾æ¨¡å¼å¯ä»¥é¿å 大éçnewæä½ï¼å 为æ¯ä¸æ¬¡newæä½é½ä¼æ¶èå åèµæºåç³»ç»èµæºã
ããè¿æ¯æäºæ½è±¡ï¼ç»åºä»£ç ç段ã
ãã使ç¨ä¼ ç»æ¹å¼ç¼ç
ããå¤å¶ä»£ç 代ç å¦ä¸:
ãã<?php
ãã......
ãã//åå§åä¸ä¸ªæ°æ®åºå¥æ
ãã$db = new DB(...);
ãã//æ¯å¦æ个åºç¨åºæ¯æ¯æ·»å ä¸æ¡ç¨æ·ä¿¡æ¯ï¼
ãã$db->addUserInfo();
ãã......
ãã//ç¶èæ们å¨å¦å¤ä¸ä¸ªå°æ¹å¯è½è¦æ¥æ¾ç¨æ·çä¿¡æ¯ï¼è¿ä¸ªæ æ¯åºç°å¨ä¸ä¸ªå½æ°ä¸ï¼è¿æ¶è¦ç¨å°æ°æ®åºå¥æèµæºï¼æ们å¯è½éè¦è¿ä¹å»å
ãã......
ããfunction test(){
ãã......
ãã//è¿æ¶æ们ä¸å¾ä¸éæ°åå§åä¸ä¸ªæ°æ®åºå¥æ,è¯æ³å¤ä¸ªåºç¨åºæ¯ä¸ï¼è¿æ ·ç代ç æ¯å¤ä¹å¯æåï¼ï¼
ãã$db = new DB(...);
ãã$db->getUserInfo();
ãã......
ãã//æäºæåæ许ä¼è¯´ï¼æä¹å¯ä»¥ä¸è¿æ ·ååï¼æç´æ¥å©ç¨globalå ³é®åä¸å°±å¯ä»¥äºåï¼çç¡®ï¼globalå¯ä»¥è§£å³é®é¢ï¼ä¹èµ·å°äºåä¾æ¨¡å¼çä½ç¨ï¼ä½æ¯OOPä¸ï¼æ们æç»è¿æ ·æ¥ç¼å代ç ï¼å 为globalåå¨å®å ¨éæ£ï¼è¯·åèç¸å ³ä¹¦ç±ï¼åæ¶åä¾æ¨¡å¼æ°æ°æ¯å¯¹å ¨å±åéçä¸ç§æ¹è¿ï¼é¿å äºé£äºåå¨å¯ä¸å®ä¾çå ¨å±åé污æå½å空é´
ããglobal $db; //OOPä¸ï¼æ们ä¸æå¡è¿æ ·ç¼å代ç
ãã......
ãã}
ãã使ç¨åä¾æ¨¡å¼ç¼ç
ããå¤å¶ä»£ç 代ç å¦ä¸:
ãã<?php
ãã......
ãã//ææçåºç¨æ æ¯åªæä¸ä¸ªæ°æ®åºå¥æèµæºï¼å¿å¿ï¼æçèé«äºï¼
ãã//èµæºä¹å¤§å¤§çå¾å°èçï¼ä»£ç ç®æ´æäºï¼ï¼
ããDB::getInstance()->addUserInfo();
ããDB::getInstance()->getUserInfo();
ãã......
ããHowâå¦ä½æ¥ç¼åPHPåä¾æ¨¡å¼ï¼
ããå¨äºè§£äºåä¾æ¨¡å¼çåºç¨åºæ¯ä¹åï¼ä¸é¢æ们éè¿ç¼ååä¾æ¨¡å¼çå ·ä½å®ç°ä»£ç æ¥ææ¡PHPåä¾æ¨¡å¼çæ ¸å¿è¦ç¹ï¼ä»£ç å¦ä¸ï¼
ããå¤å¶ä»£ç 代ç å¦ä¸:
ãã<?php
ãã/**
ãã* PHPåä¾æ¨¡å¼æ¼ç¤ºä¸¾ä¾
ãã* @author guohua.li
ãã* @modify 2010-07-11
ãã* @website http://blog.163.com/lgh_2002/
ãã*/
ããclass User{
ãã/**
ãã* éææååé ä¿åå ¨å±å®ä¾
ãã* @access private
ãã*/
ããstatic private $_instance = NULL;
ãã/**
ãã* ç§æåæé å½æ°ï¼é²æ¢å¤çå®ä¾å对象
ãã*/
ããprivate function __construct() {}
ãã/**
ãã* ç§æåå éå½æ°ï¼é²æ¢å¤çå é对象
ãã*/
ããprivate function __clone(){}
ãã/**
ãã* éææ¹æ³, åä¾ç»ä¸è®¿é®å ¥å£
ãã* @return object è¿å对象çå¯ä¸å®ä¾
ãã*/
ããstatic public function getInstance() {
ããif (is_null(self::$_instance) || !isset(self::$_instance)) {
ããself::$_instance = new self();
ãã}
ããreturn self::$_instance;
ãã}
ãã/**
ãã* æµè¯æ¹æ³: è·åç¨æ·åå
ãã*/
ããpublic function getName() {
ããecho 'hello liguohua!';
ãã}
ãã}
ããä»ä»¥ä¸ä»£ç ä¸ï¼æ们æ»ç»åºPHPåä¾æ¨¡å¼å®ç°çæ ¸å¿è¦ç¹æå¦ä¸ä¸æ¡ï¼
ãã1.éè¦ä¸ä¸ªä¿åç±»çå¯ä¸å®ä¾çéææååéï¼é常为$_instanceç§æåéï¼
ãã2.æé å½æ°åå éå½æ°å¿ 须声æ为ç§æçï¼è¿æ¯ä¸ºäºé²æ¢å¤é¨ç¨åºnewç±»ä»è失å»åä¾æ¨¡å¼çæä¹
ãã3.å¿ é¡»æä¾ä¸ä¸ªè®¿é®è¿ä¸ªå®ä¾çå ¬å ±çéææ¹æ³ï¼é常为getInstanceæ¹æ³ï¼ï¼ä»èè¿åå¯ä¸å®ä¾çä¸ä¸ªå¼ç¨
ããPHPåä¾æ¨¡å¼ç缺ç¹
ããä¼æå¨ç¥ï¼PHPè¯è¨æ¯ä¸ç§è§£éåçèæ¬è¯è¨ï¼è¿ç§è¿è¡æºå¶ä½¿å¾æ¯ä¸ªPHP页é¢è¢«è§£éæ§è¡åï¼ææçç¸å ³èµæºé½ä¼è¢«åæ¶ãä¹å°±æ¯è¯´ï¼PHPå¨è¯è¨çº§å«ä¸æ²¡æåæ³è®©æ个对象常驻å åï¼è¿åasp.netãJavaçç¼è¯åæ¯ä¸åçï¼æ¯å¦å¨Javaä¸åä¾ä¼ä¸ç´åå¨äºæ´ä¸ªåºç¨ç¨åºççå½å¨æéï¼åéæ¯è·¨é¡µé¢çº§çï¼çæ£å¯ä»¥åå°è¿ä¸ªå®ä¾å¨åºç¨ç¨åºçå½å¨æä¸çå¯ä¸æ§ãç¶èå¨PHPä¸ï¼ææçåéæ 论æ¯å ¨å±åéè¿æ¯ç±»çéææåï¼é½æ¯é¡µé¢çº§çï¼æ¯æ¬¡é¡µé¢è¢«æ§è¡æ¶ï¼é½ä¼éæ°å»ºç«æ°ç对象ï¼é½ä¼å¨é¡µé¢æ§è¡å®æ¯åè¢«æ¸ ç©ºï¼è¿æ ·ä¼¼ä¹PHPåä¾æ¨¡å¼å°±æ²¡æä»ä¹æä¹äºï¼æ以PHPåä¾æ¨¡å¼æè§å¾åªæ¯é对å次页é¢çº§è¯·æ±æ¶åºç°å¤ä¸ªåºç¨åºæ¯å¹¶éè¦å ±äº«åä¸å¯¹è±¡èµæºæ¶æ¯é常ææä¹çã
温馨提示:答案为网友推荐,仅供参考
第1个回答 2016-12-25
里的意图描述为: 保证一个类仅有一个实例, 并提供一个访问它的全局访问点.