PHP static的一個小技巧
某些單值得情況下調(diào)用一個方法我們經(jīng)常這樣寫
$a = A:: getInstance();
$a->setCount(10);
$a->showCount();
我們可以將setCount變成一個static 方法
把A:: getInstance() 放進這個方法里
我們就可以這樣來寫
A::setCount(10);
三行的代碼變成一行了,
類的實現(xiàn)如下:
<?
class A
{
private $_count = "";
static private $_instance = null;
private function __construct( )
{
}
public function showCount()
{
echo $this->_count;
}
static public function setCount( $num )
{
$obj = self::getInstance();
$obj->_setCount($num);
return $obj;
}
private function _setCount( $num )
{
$this->_count += $num;
}
static private function getInstance()
{
if ( is_null(self::$_instance) )
self::$_instance = new self();
return self::$_instance;
}
}
A::setCount(5);
A::setCount(10)->showCount();
?>
相關文章
贏政天下 賀歲二〇〇七 商業(yè)軟件大禮包 DVD 下載
贏政天下 賀歲二〇〇七 商業(yè)軟件大禮包 DVD 下載...2007-02-02PDF編輯器 Foxit PDF Editor 簡體中文版附圖文使用教程 下載
筆者向大家介紹的是它的一種編輯器—“Foxit PDF Editor”,利用它就好像使用Word一樣,從無到有的創(chuàng)建出新的一篇PDF文件,非常的簡單而實效!2007-04-04微軟Word 2007數(shù)學插件 Microsoft Math 提供下載
微軟Word 2007數(shù)學插件 Microsoft Math 提供下載...2007-11-11系統(tǒng)備份必備軟件之一鍵GHOST v8.3 Build 070101 全版下載
系統(tǒng)備份必備軟件之一鍵GHOST v8.3 Build 070101 全版下載...2007-02-02微軟最新版的 Windows Server 2008 RC0 官方下載地址
微軟最新版的 Windows Server 2008 RC0 官方下載地址...2007-09-09Ghost Windows XP SP2 龍卷風純凈版 V1.2 下載地址
Ghost Windows XP SP2 龍卷風純凈版 V1.2 下載地址...2007-10-10