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(); 
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 Commented Oct 6, 2020 at 6:34
  • 2
    Something maybe worth a read - PHP method chaining? Commented Oct 6, 2020 at 6:48
  • Does this answer your question? PHP method chaining? Commented Oct 6, 2020 at 9:14

1 Answer 1

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(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.