Last active
December 10, 2016 03:20
-
-
Save recca0120/c4a7de7e0838f6fb03edc604aedee855 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 PrivateTester | |
{ | |
public $object; | |
public function __construct($object) | |
{ | |
$this->object = $object; | |
} | |
public function __get($name) | |
{ | |
$reflectionObject = new ReflectionObject($this->object); | |
$reflectionProperty = $reflectionObject->getProperty($name); | |
$reflectionProperty->setAccessible(true); | |
return $reflectionProperty->getValue($this->object); | |
} | |
public function __call($method, $parameters) | |
{ | |
$reflectionObject = new ReflectionObject($this->object); | |
$reflectionMethod = $reflectionObject->getMethod($method); | |
$reflectionMethod->setAccessible(true); | |
return $reflectionMethod->invokeArgs($this->object, $parameters); | |
} | |
} | |
class Foo | |
{ | |
private $bar = 'bar'; | |
private function fuzz() | |
{ | |
return 'fuzz'; | |
} | |
protected function buzz() | |
{ | |
return 'buzz'; | |
} | |
} | |
$tester = new PrivateTester(new Foo()); | |
echo $tester->bar; | |
echo $tester->fuzz(); | |
echo $tester->buzz(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment