Last active
August 29, 2015 14:25
-
-
Save Schlaefer/0c52b4477e58519a985e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class A { | |
protected $_properties = [ | |
'a' => 1, | |
'b' => 2 | |
]; | |
public function &get($property) | |
{ | |
if (!strlen((string)$property)) { | |
throw new InvalidArgumentException('Cannot get an empty property'); | |
} | |
$value = null; | |
$method = '_get' . ucfirst($property); | |
if (isset($this->_properties[$property])) { | |
$value =& $this->_properties[$property]; | |
} | |
if (is_callable([$this, $method])) { | |
$result = $this->{$method}($value); | |
return $result; | |
} | |
return $value; | |
} | |
public function &__get($property) | |
{ | |
return $this->get($property); | |
} | |
public function _getFoo() { | |
return $this->_properties['b']; | |
} | |
} | |
$a = new A; | |
$array = [$a]; | |
foreach ($array as &$value) { | |
var_dump($value->a); | |
unset($value->a); | |
var_dump($value->a); | |
var_dump($value->get('foo')); | |
unset($value->foo); | |
var_dump($value->get('foo')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment