Skip to content

Instantly share code, notes, and snippets.

@Nayjest
Last active June 15, 2023 14:43
Show Gist options
  • Save Nayjest/8972170 to your computer and use it in GitHub Desktop.
Save Nayjest/8972170 to your computer and use it in GitHub Desktop.
PHP Wrapper class for accessing protected/private members outside class
<?php
class FullAccessWrapper
{
protected $_self;
protected $_refl;
public function __construct($self)
{
$this->_self = $self;
$this->_refl = new ReflectionObject($self);
}
public function __call($method, $args)
{
$mrefl = $this->_refl->getMethod($method);
$mrefl->setAccessible(true);
return $mrefl->invokeArgs($this->_self, $args);
}
public function __set($name, $value)
{
$prefl = $this->_refl->getProperty($name);
$prefl->setAccessible(true);
$prefl->setValue($this->_self, $value);
}
public function __get($name)
{
$prefl = $this->_refl->getProperty($name);
$prefl->setAccessible(true);
return $prefl->getValue($this->_self);
}
public function __isset($name)
{
$value = $this->__get($name);
return isset($value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment