WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML


 

PHP 7 Anonymous Classes


Anonymous classes can now be defined using new class. Anonymous class can be used in place of a full class definition.

Example


<?php
   interface messanger {
      public function log(string $msg);
   }

   class Application {
      private $logger;

      public function getLogger(): messanger {
         return $this->logger;
      }

      public function setLogger(messanger $logger) {
         $this->logger = $logger;
      }  
   }

   $app = new Application;
   $app->setLogger(new class implements messanger {
      public function log(string $msg) {
         print($msg);
      }
   });

   $app->getLogger()->log(This is  programming");
?>

The following output will be given:

This is  programming