I have a nested PHP class which I can access by
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;
}
The class may have up to 50 or so books so I need to create the "bookX" dynamically. I searched and found how to create variable names dynamically.
So I did this
public function getbookname($bookno){
return ${'$this->book' . $bookno. '->bookname'};
}
But it does not work, I get the following error:
Notice: Undefined variable: $this->book1->bookname in
C:\wamp\www\htdocs\lib4\includes\oop_book.php on line 32
As you can see $this->book1->bookname
is a correct method call.
I also tried
public function getbookname($bookno){
$methodreturn = ${'$this->book' . $bookno. '->bookname'};
return $methodreturn;
}
With the same result. Please help.