PHP Anonymous Class

PHP 7 supports instantiating an anonymous class throughnew class, which can be used to replace some "burn after use" Complete class definition

Instance

logger; } public function setLogger(Logger $logger) { $this->logger = $logger; } } $app = new Application; // 使用 new class 创建匿名类 $app->setLogger(new class implements Logger { public function log(string $msg) { print($msg); } }); $app->getLogger()->log("我的第一条日志"); ?>

The execution output of the above program is:

My first log