Skip to content

Instantly share code, notes, and snippets.

@anjanesh
Created January 5, 2013 11:59
Show Gist options
  • Save anjanesh/4461212 to your computer and use it in GitHub Desktop.
Save anjanesh/4461212 to your computer and use it in GitHub Desktop.
StackOverflow closed my question http://anjane.sh/Z6G4YX while I got it answered at devnetwork http://anjane.sh/RxY6Vh
<?php
class A
{
protected $_data;
public function __get($name)
{
if (in_array($name, ['Firstname', 'Lastname']))
return $this->_data[$name];
}
public function __set($name, $value)
{
if (in_array($name, ['Firstname', 'Lastname']))
{
$this->_data[$name] = $value;
return;
}
}
}
class B extends A
{
public function __get($name)
{
$value = parent::__get($name);
# echo gettype($value)."\n";
if ($value) return $value; # If not set, $value would be NULL
if (in_array($name, ['City', 'Zipcode']))
return $this->_data[$name];
}
public function __set($name, $value)
{
parent::__set($name, $value);
if (in_array($name, ['City', 'Zipcode']))
{
$this->_data[$name] = $value;
return;
}
}
}
$b = new B();
$b->Firstname = "John";
$b->Lastname = "Smith";
$b->City = "London";
$b->Zipcode = "W11 2BQ";
echo $b->Firstname."\n";
echo $b->Lastname."\n";
echo $b->City."\n";
echo $b->Zipcode."\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment