Skip to content

Instantly share code, notes, and snippets.

@Schlaefer
Last active August 29, 2015 14:25
Show Gist options
  • Save Schlaefer/0c52b4477e58519a985e to your computer and use it in GitHub Desktop.
Save Schlaefer/0c52b4477e58519a985e to your computer and use it in GitHub Desktop.
<?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