2011-05-31 39 views
0

-EDIT-是的,這確實有用。我現在看到...PHP。可以使用變量作爲對象嗎?

是否可以使用變量來確定屬性?

我有2類,被稱爲我的控制器的一部分

$this->document->setPageNum 

$this->document2->setPageNum 

我想用類似

if (is_array($pagenum)) { 
    $doc = 'document'; 
} else { 
    $doc = 'document2'; 
} 

$this->$doc->setPageNum = $pagenum; 

這是可能做到?

+3

不會是一個有趣的經驗,只是嘗試一下? o.o – Populus 2011-05-31 04:17:27

+0

啊。該死..我嘗試過,並沒有工作。沒有意識到我有錯字。它確實像我希望的那樣工作。謝謝! – Dss 2011-05-31 04:20:30

+1

確實,[確實有效](http://www.ideone.com/0uia5)。 ;-) – 2011-05-31 04:29:56

回答

3

爲什麼不救自己的代碼混亂的麻煩,只需設置變量爲要實際的對象,像這樣:

if (is_array($pagenum)) { 
    $doc = $this->document; 
} else { 
    $doc = $this->document2; 
} 

$doc->setPageNum = $pagenum; 
+0

這會更有意義..好的呼叫。謝謝 – Dss 2011-05-31 04:20:55

相關問題