Will it reckon with good practice ...
I have class A with the following definitions:
class A{
private $_varOne;
private $_varTwo;
private $_varThree;
public $varOne;
public function __get($name){
$fn_name = 'get' . $name;
if (method_exists($this, $fn_name)){
return $this->$fn_name();
}else if(property_exists('DB', $name)){
return $this->$name;
}else{
return null;
}
}
public function __set($name, $value){
$fn_name = 'set' . $name;
if(method_exists($this, $fn_name)){
$this->$fn_name($value);
}else if(property_exists($this->__get("Classname"), $name)){
$this->$name = $value;
}else{
return null;
}
}
public function get_varOne(){
return $this->_varOne . "+";
}
}
$A = new A();
$A->_varOne;
$A->_varTwo;
In order not to create 4 set and 4 get methods, I used magic methods to either call the respected getter for the property I need, or simply return the value of the property without any changes. Could this be considered good practice
source
share