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.

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) {
$app->getLogger()->log("My first Log Message");



My first Log Message


Thank You for visit Design & Develop by Abdur Rahaman