kwakefield's avatar

When to return this vs no value

I'm wondering if there's a guideline/best practice for when to return $this vs. no value from a model method. I've seen both in the video tutorials ... eg. do something and return $this ... vs ... do something with no return value. I understand returning $this when you know you need to chain methods. Just wondering how to decide if you don't know if it will make sense to use the method in a chain.

0 likes
2 replies
d3xt3r's avatar

Its your code, you will need to decide, right?

Plus, anyway, its mostly used for setters (fluent), so even if you have $this returned for all of them, makes no harm.

kwakefield's avatar

Thanks d3xt3r. Part of me hates trying to decide these things. Sticking to some kind of standard not only means I don't have choose, but makes the code a little easier to predict for the next person who has to maintain it. I was hoping someone else had already done the hard thinking :)

Please or to participate in this conversation.