Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP Anonymous Class

PHP Anonymous Class

PHP 7 New Features PHP 7 New Features

PHP 7 supports instantiating an anonymous class via new class to instantiate an anonymous class, which can be used to replace some "use-and-throw-away" full class definitions.

Example

Example

logger ; } public function setLogger ( Logger $logger ) { $this -> logger = $logger ; } } $app = new Application ; // create an anonymous class using new class $app -> setLogger (new class implements Logger { public function log ( string $msg ) { print( $msg ); } }); $app -> getLogger ()-> log ( "My first log" ); ?>

The output of the above program is:



我的第一条日志


PHP 7 New Features PHP 7 New Features