Last active
August 19, 2022 04:15
-
-
Save mikedamoiseau/98da7fba7156423b504483531f9dd618 to your computer and use it in GitHub Desktop.
PHP Access a private property of an object
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 | |
/** | |
* In this example, we're binding the `$this` value of the closure to the object. | |
*/ | |
class Foo { | |
private $secret = 'foo'; | |
} | |
$foo = new Foo(); | |
$secret = (fn() => $this->secret)->call($foo); | |
echo $secret; // will output 'foo' | |
// ================================================ | |
class Kitchen { | |
private $yummy = 'cake'; | |
} | |
$kitchen = new Kitchen(); | |
$sweetsThief - Closure::bind(function (Kitchen $kitchen) { | |
return $kitchen->yummy; | |
}, null, 'Kitchen'); | |
echo $sweetsThief($kitchen); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment