统计在线人数...

php4和php5单态模式(Singleton Pattern)写法

[ 来源:如梦令 | 作者:佚名 | 时间:2008-4-11 下午 09:29:42 | 浏览:统计中... ]

单态模式(Singleton Pattern) 就是一个类Class只有一个实例存在。(Ensure a class only has one instance, and provide a global point of access to it.)
这个是php5的写法。

以下为引用的内容:
<?php
class SingletonPhp5{
 static private $_instance=null;

 function getInstance(){
  if(! self::$_instance){
   self::$_instance=new self;
  }
  return self::$_instance;
 }

 function __construct(){

 }

 function Show(){
  echo 'Singleton on Php5';
 }
}

{
 $Singleton=SingletonPhp5::getInstance()->Show();
}

这个是php4的写法,当然此方法在php5下也可以正常运行。

以下为引用的内容:

class SingletonPhp4{      
   function &getInstance(){          
     static $_instance=array();          
     if(empty($_instance)){              
         $_instance[]= & new SingletonPhp4();  
        
}          
  return $_instance[0];      

  }
        
function SingletonPhp4(){        

}        

function Show(){          
   echo 'Singleton on Php4';      
   }  
}    

{      
   $Singleton=SingletonPhp4::getInstance();      
   $Singleton->Show();  
}
 

共有0人参与评价,平均得分:0分
评论内容只代表网友观点,与本站立场无关! 查看完整内容
   

当前在线人数
QQ:748838 MSN:allen_xia#msn.com E-mail:allenxia666#126.com QQ群:站长联盟北方区-北京(28200145) 站长联盟南方区-上海(67713522)