Reputation: 5957
I am reading the documentation of php about Variable functions
I don't understand the example 4 below.
How come the first and second $func();
can call the function bar()
and baz()
. The $func
is a array
.
Maybe it is a silly question. Thanks.
<?php
class Foo
{
static function bar()
{
echo "bar\n";
}
function baz()
{
echo "baz\n";
}
}
$func = array("Foo", "bar");
$func(); // prints "bar"
$func = array(new Foo, "baz");
$func(); // prints "baz"
$func = "Foo::bar";
$func(); // prints "bar"
?>
Upvotes: 1
Views: 61
Reputation: 12131
Have a look at the documentation of callables - if an array is used, the first element references the class name to be used, the second element references the method name to be called. This means, the two last lines are equal:
$func = array("Foo", "bar");
$func(); // prints "bar"
Foo::bar(); // also prints "bar"
Upvotes: 2