我有,我可以通過如何嵌套的PHP類
public function getbookname($bookno){
$namex='No book';
switch($bookno){
case 1:
$namex=$this->book1->bookname;
break;
case 2:
$namex=$this->book2->bookname;
break;
case 3:
$namex=$this->book3->bookname;
break;
}
return $namex;
}
訪問一個嵌套的PHP類的類最多可有50點左右的書,所以我需要打造的「使用動態變量名bookX「動態地。我搜索並找到了how to create variable names dynamically。
所以我做了這個
public function getbookname($bookno){
return ${'$this->book' . $bookno. '->bookname'};
}
但它不工作,我得到以下錯誤:
Notice: Undefined variable: $this->book1->bookname in
C:\wamp\www\htdocs\lib4\includes\oop_book.php on line 32
正如你可以看到$this->book1->bookname
是一個正確的方法調用。
我也試過
public function getbookname($bookno){
$methodreturn = ${'$this->book' . $bookno. '->bookname'};
return $methodreturn;
}
具有相同的結果。請幫忙。
嘗試'$ BOOKNAME = '書'。 $ bookno;返回$ this - > $ bookname-> bookname;' – Rufinus 2014-09-29 11:17:11