Created
August 26, 2013 22:11
-
-
Save evansolomon/6347288 to your computer and use it in GitHub Desktop.
Closure::bindTo() example
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
<?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