如何在调用其他静态函数时调用函数?

How can I invoke a function automatically when any static function is called once.

class A{
    private static $val;
    public static function X(){
        return static::$val;
    }
}

class B extend A
{    

}

Is it possible if I call B::X(); then a function can set the value of $val of parent class. This has to be done without creating a instance of class B.

I wanted to do this automatically. something like what construct does. Before any static method is called I want to invoke a function automatically

Is it possible if I call B::X(); then a function can set the value of $val of parent class. This has to be done without creating a instance of class B

Try this:

class A {
    protected static $val;
    public static function X(){
        return static::$val;
    }
}

class B extends A
{
    public static function X(){
        static::$val = 123;
        return parent::X();
    }
}

echo B::X();

You have to

  • Replace private static $val; with protected static $val; to make property visible in class B
  • You have to use extends instead of extend
  • You can use parent:: to call a method of parent class (In your case class A)