Home > Article > Backend Development > Detailed explanation of new features of PHP: anonymous class
This article mainly shares with you a detailed explanation of the new features of PHP, the anonymous class, and I hope it can help you.
Code
<?phpinterface Logger { public function log(string $msg);}class Application { private $logger; public function getLogger(): Logger { return $this->logger; } public function setLogger(Logger $logger) { $this->logger = $logger; } }$app = new Application;$app->setLogger(new class implements Logger { public function log(string $msg) { echo $msg; } }); var_dump($app->getLogger());?>
Result
object(class@anonymous)#2 (0) {}
Related recommendations:
Introduction to new features such as anonymous classes, import classes and closure usage in php7
The above is the detailed content of Detailed explanation of new features of PHP: anonymous class. For more information, please follow other related articles on the PHP Chinese website!