Welcome Guest, Not a member yet? Register   Sign In
Setter / Getter Best Practice 2016
#3

In order for the method to be chain-able, you should always return $this. Otherwise, you can run into some issues down the road. For example, if you call the method like this:

PHP Code:
$this->plugin_prefix($someVar)->extension($someOtherVar)->something_else($thirdVar); 

If $someVar happens to be null for some reason, you have an error, because plugin_prefix() will return $this->plugin_prefix instead of returning $this, and you probably won't be able to continue down the chain (or if you can continue down the chain, the value of $this->plugin_prefix is probably not what other parts of the code will expect it to be).
Reply


Messages In This Thread
Setter / Getter Best Practice 2016 - by dmyers - 07-25-2016, 04:08 PM
RE: Setter / Getter Best Practice 2016 - by mwhitney - 07-27-2016, 09:55 AM



Theme © iAndrew 2016 - Forum software by © MyBB