Skip to content

Instantly share code, notes, and snippets.

@yoeunes
Last active February 20, 2020 14:06
Show Gist options
  • Save yoeunes/8a2725f8abe5a46b07394116deae789a to your computer and use it in GitHub Desktop.
Save yoeunes/8a2725f8abe5a46b07394116deae789a to your computer and use it in GitHub Desktop.
access private fields of a class using closures
<?php
class Article
{
private string $title = 'private title';
private function callingPrivate(string $query)
{
echo $query, PHP_EOL;
}
}
$closure = function () {
return $this->title;
};
$article = new Article();
$title = $closure->bindTo($article, Article::class)();
echo $title, PHP_EOL;
$closure = function(string $query) {
$this->callingPrivate($query);
};
$closure->call($article, 'calling private methods');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment