Last active
December 23, 2015 23:09
-
-
Save krakjoe/6707673 to your computer and use it in GitHub Desktop.
anon classes demo
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
<pre> | |
<?php | |
class Outer { | |
protected $data; | |
public function __construct(&$data) { | |
/* array access will be implemented by the time we get to here */ | |
$this->data = &$data; | |
} | |
public function getArrayAccess() { | |
/* create a child object implementing array access */ | |
/* this grants you access to protected methods and members */ | |
return new class extends Outer implements ArrayAccess { | |
public function offsetGet($offset) { return $this->data[$offset]; } | |
public function offsetSet($offset, $data) { return ($this->data[$offset] = $data); } | |
public function offsetUnset($offset) { unset($this->data[$offset]); } | |
public function offsetExists($offset) { return isset($this->data[$offset]); } | |
}($this->data); | |
} | |
} | |
$outer = new Outer($_SERVER); | |
$proxy = $outer->getArrayAccess(); | |
var_dump ($proxy); | |
unset ($proxy["argv"]); | |
var_dump ($outer); | |
var_dump ($proxy); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://gist.ushow.us/krakjoe/6707673