4 ms·
> Except at some point you will need setModel() to fail for some arguments, e.g. setModel('Trabant'), and you will end up with some methods returning $this and
by Matt3o12_ 9y ago
> Except at some point you will need setModel() to fail for some arguments, e.g. setModel('Trabant'), and you will end up with some methods returning $this and some not. This is not going to be a "clean code" by any measure.
Thankfully PHP now supports exceptions, which all chain method APIs have used exclusively used. It would be hell if they a mixed value consisting of true in case everything is ok or any other type (including strings, ints and custom objects) depending on the return code. If you used such an API, you would have spaghetti code because every other line would be if ($err !== true) { handleErr }
It is just much easier to have a try and catch block and use multiple catch and try blocks if you need more fine grand controller where the error occoried and the exception does not contain that detail.
So in a way, chained method should always garantuee to return itself or throw an appropriate error.
- megous 9y ago"Now" is like for the last 12 years. With a few lines you could convert errors to exceptions globally using 1 callback.