Setter / Getter Best Practice 2016 |
This is actually part of the principle of Command-query separation, as the Single-Responsibility Principle is dealing with the whole object, rather than individual methods.
Method chaining in PHP generally requires that the return values of your methods are consistent, regardless of input (and, in some cases, regardless of state). We usually support this by returning $this in methods which otherwise would not return anything, or might otherwise return some indication of whether an error occurred. A getter method can still be used in a chain, but only if it always returns an object of the expected type. If a method does not return the expected type (whether it is $this from a setter or some other object from a getter), you need to check the return type (which breaks the chain) or wrap it in a try/catch block in case the return is the wrong type and an Exception is thrown when you try to access the next method in the chain (in which case it becomes harder to recover). So, chaining is fine, but it requires a certain level of commitment to the return values of your methods. |
Messages In This Thread |
Setter / Getter Best Practice 2016 - by dmyers - 07-25-2016, 04:08 PM
RE: Setter / Getter Best Practice 2016 - by versalle88 - 07-26-2016, 08:36 AM
RE: Setter / Getter Best Practice 2016 - by dmyers - 07-27-2016, 11:40 AM
RE: Setter / Getter Best Practice 2016 - by mwhitney - 07-27-2016, 09:55 AM
RE: Setter / Getter Best Practice 2016 - by mwhitney - 07-27-2016, 01:09 PM
|