PHP 7 supports instantiating an anonymous class through new class, which can be used to replace some "burn after use" complete class definitions.
Example
<?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 above program execution output result is:
我的第一条日志















The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~ 