PHP Anonymous Class

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

Instance

<?php
interface 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;
// 使用 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