简体   繁体   中英

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.

Magic methods, such as __call and __callStatic Docs are only called for methods that do not exist in your class. 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 . Of course, returned values of these functions should also be returned by magic methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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