Access to undeclared method in class with php

I am trying to understand this line of code:

$ret = $box->$command();

The method command is not defined in the $ box field, and it is strange that there is a $before command . I just do not understand.

+4
source share
3 answers

Executes a method with the name stored in the variable of the $commandobject stored in $box.

So, suppose the class $boxhas a method called foo, this will work:

$command = "foo";
$box->$command();

and will be equivalent

$box->foo();

, , . $command, , , - ( php-).

+4
$foo = 'bar';
$obj->$foo(); // calls the bar() method

.

+4

$command will be a string whose value is the name of the method in this class definition.

+2
source

All Articles