简体   繁体   中英

call public method after static method in php

I have class sample and first call static method and return to call other public method:

class foo{
 public static $var;
    public static function first()
    {
       self::$var = 1;
       return self::class;
    }  
    public function second()
    {
       return self::$var;
    }
}

and I need call public method after static method sample here:

foo::first()->second();

No, you can't do that, because from your static method you return just the string - class name. For calling your public method, you need that static method is returning an instance of your object, like this

return new self();

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