PHP 7 – Anonymous Classes

Anonymous Classes

Anonymous Classes 

Anonymous classes can now be defined using new class. Anonymous class can be used in place of a full class definition.  Anonymous classes are useful when simple, one-off objects need to be created.

<?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;
$app->setLogger(new class implements Logger {
public function log(string $msg) {
print($msg);
}
});
$app->getLogger()->log("My first Log Message");
?>

 

Result

My first Log Message

 

Thank You for visit Webriceter.com Design & Develop by Abdur Rahaman