Last active
February 22, 2018 09:17
-
-
Save ScreamingDev/2958f589eaeeef2fcc787c2878b8057a to your computer and use it in GitHub Desktop.
SUDO Object
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
<?php | |
class SudoObject | |
{ | |
/** | |
* @var object | |
*/ | |
private $instance; | |
/** | |
* @var \ReflectionObject | |
*/ | |
protected $reflection; | |
public function __construct(object $instance) | |
{ | |
$this->instance = $instance; | |
$this->reflection = new \ReflectionObject($instance); | |
} | |
private function getProperty($property): \ReflectionProperty | |
{ | |
$reflectionProperty = $this->reflection->getProperty($property); | |
if (!$reflectionProperty->isPublic()) { | |
$reflectionProperty->setAccessible(true); | |
} | |
return $reflectionProperty; | |
} | |
public function __get($name) | |
{ | |
return $this->getProperty($name)->getValue($this->instance); | |
} | |
public function __set($name, $value) | |
{ | |
$this->getProperty($name)->setValue($this->instance, $value); | |
} | |
public function __isset($name) | |
{ | |
return $this->reflection->hasProperty($name); | |
} | |
public function __call($name, $arguments) | |
{ | |
return $this->getMethod($name)->invokeArgs($this->instance, $arguments); | |
} | |
private function getMethod($name): \ReflectionMethod | |
{ | |
$reflectionMethod = $this->reflection->getMethod($name); | |
if (!$reflectionMethod->isPublic()) { | |
$reflectionMethod->setAccessible(true); | |
} | |
return $reflectionMethod; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment