Last active
December 22, 2015 05:08
-
-
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
This file contains 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
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