Last active
February 20, 2020 14:06
-
-
Save yoeunes/8a2725f8abe5a46b07394116deae789a to your computer and use it in GitHub Desktop.
access private fields of a class using closures
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 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