Skip to content

Instantly share code, notes, and snippets.

@jdrda
Last active November 24, 2018 01:02
Show Gist options
  • Save jdrda/a9f0433af2291b2fea94af5871e48a8d to your computer and use it in GitHub Desktop.
Save jdrda/a9f0433af2291b2fea94af5871e48a8d to your computer and use it in GitHub Desktop.
General setter with possibility of set default value suitable for writing other setters
class Product
{
/**
* Product name to be set
*
* @var string
*/
private $_name;
/**
* General setter prototype
*
* @param $variable
* @param $value
* @param $defaultValue
* @return bool If true - value has been set, if false - default value used
*/
private function _generalSetter($variable, $value, $defaultValue){
if(empty($value) === false){
$variable = $value;
return true;
}
else{
$variable = $defaultValue;
return false;
}
}
/**
* Setter for product name
*
* This setter uses general setter above to be more simple
*
* @param $productName
* @return bool
*/
public function setProductName($productName){
return $this->_generalSetter($this->_name, $productName, 'Box of milk');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment