简体   繁体   English

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

[英]How can i catch a call of public static function in PHP?

<?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(){}
}

I search for PHP public static function handler, I try __callStatic() but its calling only with private and protected methods. 我搜索PHP公共静态函数处理程序,我尝试__callStatic()但它只调用私有和受保护的方法。

Magic methods, such as __call and __callStatic Docs are only called for methods that do not exist in your class. 魔术方法,例如__call__callStatic Docs仅针对您的类中不存在的方法调用。 They are not triggered, when a normal method call happens. 当正常的方法调用发生时,它们不会被触发。 So the're is no way to achieve that with public methods. 因此,使用公共方法无法实现这一目标。

However, you can make these methods private/protected, and call them in magic methods with call_user_func or call_user_func_array . 但是,您可以将这些方法call_user_func private / protected,并使用call_user_funccall_user_func_array在魔术方法中调用它们。 Of course, returned values of these functions should also be returned by magic methods. 当然,这些函数的返回值也应该通过魔术方法返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM