Skip to content

Instantly share code, notes, and snippets.

@denizozturk
Created February 11, 2022 18:51
Show Gist options
  • Select an option

  • Save denizozturk/89bc0210944cd37835919d69ca7981f1 to your computer and use it in GitHub Desktop.

Select an option

Save denizozturk/89bc0210944cd37835919d69ca7981f1 to your computer and use it in GitHub Desktop.
<?php
/**
* Access/modify any instance private property.
*/
class Invade
{
public function __construct(
protected $instance,
protected $setter,
protected $getter
) {
}
public function __get($property)
{
return $this->getter->call($this->instance, $property);
}
public function __set($property, $value)
{
$this->setter->call($this->instance, $property, $value);
}
}
/**
* EXAMPLE:
*
* class Foo {
* private $property = 'bar';
* }
*
* $foo = new Foo();
* $invade = new Invade($foo);
*
* echo $invade->property; // bar
* $invade->property = 'foo bar';
* echo $invade->property; // foo bar
*
* Source: jszobody/Undies.php
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment