Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ScreamingDev/2958f589eaeeef2fcc787c2878b8057a to your computer and use it in GitHub Desktop.
Save ScreamingDev/2958f589eaeeef2fcc787c2878b8057a to your computer and use it in GitHub Desktop.
SUDO Object
<?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