如何在PHP中捕获公共静态函数的调用?

时间:2012-07-20 18:14:24

标签: php static magic-methods

<?php
class A
{
    public static function foo()
    {
        // some action
    }

    public static function __callStatic($method, $args)
    {
        self::foo();
        static::$method($args);
    }
}

class B extends A
{
    public static function bar(){}
}

我搜索PHP公共静态函数处理程序,我尝试__callStatic()但它只调用私有和受保护的方法。

1 个答案:

答案 0 :(得分:3)

只有在类中不存在的方法才会调用__call and __callStaticDocs等魔术方法。当正常的方法调用发生时,它们不会被触发。因此,无法通过公共方法实现这一目标。

但是,您可以将这些方法设为私有/受保护,并使用call_user_funccall_user_func_array以魔法方式调用它们。当然,这些函数的返回值也应该通过魔术方法返回。