I have a method display()
in the book class.
$name = 'display()';
$book = new Book('PHP Object-Oriented Solutions', 300);
$book->$name;
How can i call display method using $book->$name
I have a method display()
in the book class.
$name = 'display()';
$book = new Book('PHP Object-Oriented Solutions', 300);
$book->$name;
How can i call display method using $book->$name
You need to tell PHP that you're trying to execute a method, not in the variable itself, but within the actual code:
$name = 'display';
$book = new Book('PHP Object-Oriented Solutions', 300);
$book->$name();
Otherwise, as you have seen, it will treat $name
as a property name, and rightly so ... If you have both a property and a method named 'display', there wouldn't be a way to distinguish between the two using what you've tried.