Skip to content

Instantly share code, notes, and snippets.

@akovalyov
Last active December 22, 2015 05:08
Show Gist options
  • Save akovalyov/6421406 to your computer and use it in GitHub Desktop.
Save akovalyov/6421406 to your computer and use it in GitHub Desktop.
accessing private properties of parent class which are set in the parent constructor. With php 5.3
function __construct($someProperty)
{
$className = get_parent_class($this);
$reflection = new \ReflectionClass($className);
if (version_compare(phpversion(), '5.4', '<')) {
$instance = new \ReflectionObject(unserialize(
sprintf('O:%d:"%s":0:{}', strlen($className), $className)
));
} else {
$instance = $reflection->newInstanceWithoutConstructor();
}
$refProperty = $instance->getProperty($$someProperty);
$refProperty->setAccessible(true);
$refProperty->setValue($instance, $someProperty);
$this->parent = $instance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment