【发布时间】:2014-02-15 17:32:39
【问题描述】:
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();
我的期望:
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的调用中添加parent部分。看看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'));