本文主要和大家分享PHP新特性之匿名类详解,希望能帮助到大家。
代码
<?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());?>
结果
object(class@anonymous)#2 (0) {}
相关推荐:
以上是PHP新特性之匿名类详解的详细内容。更多信息请关注PHP中文网其他相关文章!