class A
{
public static function who1()
{
var_dump(get_called_class());
}
}
class B extends A
{
public static function who2()
{
parent::who1();
}
}
call_user_func(array('B', 'parent::who1'));
B::who2();
What I expect:
string 'B' (length=1)
string 'B' (length=1)
Actual returns:
boolean false
string 'B' (length=1)
Can anyone tell me why the output is different from what I expected?
see also:
https://www.php.net/manual/en/language.types.callable.php
https://www.php.net/manual/en/function.get-called-class.php
edit: Maybe my old code is not clear, here is the new example:
class A
{
public static function who()
{
var_dump(get_called_class());
}
}
class B extends A
{
public static function who()
{
echo 'hehe';
}
}
call_user_func(array('B', 'parent::who'));
why it output false?
parentpart in the call toparent::who1. Have a look at php.net/manual/en/language.oop5.inheritance.php:For example, when you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality.parent:::call_user_func(array('B', 'who1'));