Skip to content

Instantly share code, notes, and snippets.

@recca0120
Last active December 10, 2016 03:20
Show Gist options
  • Save recca0120/c4a7de7e0838f6fb03edc604aedee855 to your computer and use it in GitHub Desktop.
Save recca0120/c4a7de7e0838f6fb03edc604aedee855 to your computer and use it in GitHub Desktop.
<?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