Skip to content

Instantly share code, notes, and snippets.

@jayarjo
Created December 2, 2013 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayarjo/7750154 to your computer and use it in GitHub Desktop.
Save jayarjo/7750154 to your computer and use it in GitHub Desktop.
php: getters and setters alternative
function __set($property, $value)
{
switch ($property) {
case 'host':
if (!preg_match("{^https?://}", $value)) {
$value = "http://$value";
}
// use internal php facilities to get pure host
if ($url_parts = parse_url($value)) {
$value = $url_parts['scheme'] . '://' . $url_parts['host'];
}
$this->_host = $value;
break;
case 'options':
$this->_options = array_merge(array(
'buffer' => false,
'cb_url' => array($this, '_cb_url'),
'items_per_cb' => 1,
'cb_sitemap' => array($this, '_cb_sitemap')
), $value);
break;
}
}
function __get($property)
{
if (in_array($property, array('host', 'options'))) {
return $this->{"_$property"};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment