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

I have been doing this (see below) for sometime and wanted to know if this would be considered best practice for a PHP setter/getter function?

PHP Code:
public function plugin_prefix($prefix=null) {
 
 /* return value if no value sent in */
 
 if ($prefix === null) {
 
  return $this->plugin_prefix;
 
 }

 
 /* set file extension */
 
 $this->plugin_prefix $prefix;

 
 /* chain-able */
 
 return $this;



This allows me to set multiple settings using


PHP Code:
$this->my_object->plugin_prefix('foo')->extension('bar')->something_else('foobar'); 

and get a value with


PHP Code:
$plugin $this->my_object->plugin_prefix(); 


I look forward to your comments...

DMyers
Reply
#2

If this works well for you, I'd say it's okay. As far as best practices go, I think getters and setters should be separate methods; otherwise, you're violating the SRP:

https://en.wikipedia.org/wiki/Single_res..._principle
Reply
#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
#4

(07-26-2016, 08:36 AM)versalle88 Wrote: If this works well for you, I'd say it's okay. As far as best practices go, I think getters and setters should be separate methods; otherwise, you're violating the SRP:

https://en.wikipedia.org/wiki/Single_res..._principle

That sounds pretty good so more like 

$object->extension($ext);

and

$extension = $object->get_extension();

and no chaining??

DMyers
Reply
#5

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB