Created
November 20, 2014 15:49
-
-
Save aczietlow/fc13216a08f694bf0959 to your computer and use it in GitHub Desktop.
Accessing super class private variables
This file contains hidden or 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 MyClass { | |
| private $string = 'Can\'t touch this!'; | |
| public function doStuff() { | |
| $this->string = $this->string . ' Na Na Na Na.'; | |
| } | |
| } | |
| Class SubClass extends MyClass { | |
| public function __construct() { | |
| parent::doStuff(); | |
| } | |
| public function printStuff() { | |
| $getter = function($myClass) { | |
| return $myClass->string; | |
| }; | |
| $getter = Closure::bind($getter, NULL, new MyClass()); | |
| print $getter($this); | |
| } | |
| } | |
| $obj = new SubClass(); | |
| $obj->printStuff(); // prints stuff. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment