Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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