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();
我期待什麼:PHP類範圍與call_user_func混亂()
string 'B' (length=1)
string 'B' (length=1)
實際回報:
boolean false
string 'B' (length=1)
誰能告訴我,爲什麼輸出是跟我想象中有什麼不同?
還看到:
http://us1.php.net/manual/en/language.types.callable.php
http://us1.php.net/manual/en/function.get-called-class.php
編輯: 也許我以前的代碼不明確,這裏是新的例子:
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'));
爲什麼它輸出假?
您不需要在調用「parent :: who1」時添加「父」部分。看看http://php.net/manual/en/language.oop5.inheritance.php:'例如,當你擴展一個類時,這個子類繼承了父類的所有公共和受保護的方法。除非一個類重寫這些方法,否則它們將保留它們的原始功能。' –
不要使用'parent ::':'call_user_func(array('B','who1'));' – Wrikken