【发布时间】:2011-10-08 16:00:02
【问题描述】:
我正在尝试从其子类的成员调用静态魔术函数 (__callStatic)。问题是,它转而使用非静态__call。
<?php
ini_set("display_errors", true);
class a
{
function __call($method, $params)
{
echo "instance";
}
static function __callStatic($method, $params)
{
echo "static";
}
}
class b extends a
{
function foo()
{
echo static::bar();
// === echo self::bar();
// === echo a::bar();
// === echo b::bar();
}
}
$b = new b();
echo phpversion()."<br />";
$b->foo();
?>
输出:
5.3.6
instance
如何让它显示为“静态”?
【问题讨论】:
-
好像不可能,我找到了这个讨论:allegro.cc/forums/thread/607508/919970.
-
这里:
Interestingly, PHP 5.3.3 gives __callStatic priority, but that behavior was reverted back in PHP 5.3.4. -
Lazlo Bonin,请记住您在这里处理的是对象继承。即使你认为你有两个类,这实际上是一个。如果你这样做,那一个类不会被静态调用:
$b->foo();。这不是静态函数调用。黑客将改为这样做:$b::foo();。自己试试吧。 -
@hakre:或者
b::foo();。
标签: php methods static instance