Nikita Melnikov
Nikita Melnikov

Reputation: 209

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.

Upvotes: 1

Views: 768

Answers (1)

knittl
knittl

Reputation: 265221

Magic methods, such as __call and __callStaticDocs 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.

Upvotes: 3

Related Questions