PHP - 哪个类调用了我的静态方法?

时间:2013-02-19 21:51:45

标签: php methods static

我需要知道什么类型调用静态方法,而不发送参数

class foo
{
    public static function test($clase)
    {
        echo "Class invoke:" . FUNCTION();
    }
}

class A { public function x { foo::test(); } }
class B { public function y { foo::test(); } }
class C { public function z { foo::test(); } }

1 个答案:

答案 0 :(得分:0)

如果您将所有类扩展为foo,则可以使用后期静态绑定和get_called_class()(PHP> = 5.3),如下所示:

class foo
{
    public static function test($clase)
    {
        echo "Class invoke:" . get_called_class();
    }
}

class A extends foo { public function x() { self::test(''); } }
class B extends foo { public function y() { self::test(''); } }
class C extends foo { public function z() { self::test(''); } }

使用这些对象:

$a = new A; $a->x();
$b = new B; $b->y();
$c = new C; $c->z();

get as output

Class invoke:A
Class invoke:B
Class invoke:C