Skip to content

Instantly share code, notes, and snippets.

@evansolomon
Created August 26, 2013 22:11
Show Gist options
  • Save evansolomon/6347288 to your computer and use it in GitHub Desktop.
Save evansolomon/6347288 to your computer and use it in GitHub Desktop.
Closure::bindTo() example
<?php
class Thing {
private $name = "Evan";
}
$thing = new Thing();
class ExtendedThing extends Thing {
function say_name() {
if (isset($this->name))
echo $this->name;
}
}
$extended_thing = new ExtendedThing();
$extended_thing->say_name();
// Of course that doesn't work because $name is private.
$accessPrivateVar = function() {
echo $this->name;
};
$accessPrivateVar = $accessPrivateVar->bindTo($thing, $thing);
$accessPrivateVar();
// Prints "Evan"...and my head explodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment