Skip to content

Instantly share code, notes, and snippets.

@mikedamoiseau
Last active August 19, 2022 04:15
Show Gist options
  • Save mikedamoiseau/98da7fba7156423b504483531f9dd618 to your computer and use it in GitHub Desktop.
Save mikedamoiseau/98da7fba7156423b504483531f9dd618 to your computer and use it in GitHub Desktop.
PHP Access a private property of an object
<?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