Ok, guys. Here’s something I’ve been thinking about for a while:
As it’s possible to chain methods in PHP5, I wonder if it is possible to take the concept even further by in a smart way determining if a method is the last in the chain to be executed – and that’s without utilizing a third method named getResult()?
Normal method calling:
$myClass->dofirst(); // Data is returned from the dofirst-method
What I would like;
$myclass->dofirst()->sortOutputfromDofirstAndReturn();
The idea is that the second method, sortOutputfromDofirstAndReturn() would prevent the dofirst() method from returning, and instead perform the logic stated inside the second method, without needing a third method to bounce the return to the user.
Maybe a bit confusing so let me know if I need to clarify anything!
There are few possibilities:
Add one (or few special) methods (see Doctrien 1.2) that finish chain (
executeis special)If final result is always array like object than You can implement
Iteratorinterface with Your chain logic.Half stupid one. If Your result is string, than reimplement
__toStringmethod…