Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14343

Setter / Getter Best Practice 2016

$
0
0
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

Viewing all articles
Browse latest Browse all 14343

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>