0

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();
vahid
  • 11
  • 3
  • well you can't chain `second` method to a `string`. so it won't work. return the instance on the `first`, then chain `second`. like the answer below – Kevin Oct 06 '20 at 06:34
  • 2
    Something maybe worth a read - [PHP method chaining?](https://stackoverflow.com/questions/3724112/php-method-chaining) – Nigel Ren Oct 06 '20 at 06:48
  • Does this answer your question? [PHP method chaining?](https://stackoverflow.com/questions/3724112/php-method-chaining) – Egel Oct 06 '20 at 09:14

1 Answers1

2

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();
Ihor Kostrov
  • 2,361
  • 14
  • 23