Skip to content

Instantly share code, notes, and snippets.

@krakjoe
Last active December 23, 2015 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krakjoe/6707673 to your computer and use it in GitHub Desktop.
Save krakjoe/6707673 to your computer and use it in GitHub Desktop.
anon classes demo
<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);
?>
@krakjoe
Copy link
Author

krakjoe commented Sep 25, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment