有沒有可能在php類中引用變量時運行函數,而不是簡單地返回它的值,類似於javascript的變量持有方法的能力?在PHP類中引用實例變量時執行方法嗎?
class LazyClassTest()
{
protected $_lazyInitializedVar;
public function __construct()
{
/* // How can this call and return runWhenReferrenced() when
// someone refers to it outside of the class:
$class = new LazyClass();
$class->lazy;
// Such that $class->lazy calls $this->runWhenReferrenced each
// time it is referred to via $class->lazy?
*/
$this->lazy = $this->runWhenReferrenced();
}
protected function runWhenReferrenced()
{
if (!$this->_lazyInitializedVar) {
$this->_lazyInitializedVar = 'someValue';
}
return $this->_lazyInitializedVar
}
}
嗯......我們並非人人懂德語,那麼爲什麼不分享英文說明書? :-) – Rafid 2011-01-12 16:16:11
對不起,修正了鏈接。 – mplappert 2011-01-12 16:19:20