I’ve found some limited use in chaining class functions, say $class->setUser('foo')->getInfo() (bad example) although am having trouble understanding how to handle any errors that arise from one of the calls in the chain.
If setUser() for example came with an error, and returned false, it would not return $this and not allow another function to be called, thus displaying an error.
What I have actually just realized (and correct me if this is wrong), would throwing an exception if there is an error in setUser() prevent the following getInfo() function from running and emitting an error?
This would be useful knowledge to at least have, so that I can debug code that uses chaining even if I am not to use it.
Yes, throwing exceptions would probably be a good idea in this case. You’ll be able to pinpoint where the error happened in the chain.
Here’s an example of how that could work: