PHP7匿名类

2016-09-20 0 764
PHP7匿名类
在php7中,匿名类现在可以使用 new class 来定义。匿名类可以使用来代替完整的类定义。

示例

<?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");
?>
这将在浏览器产生输出以下结果-
My first Log Message

遇见资源网 PHP7归档 PHP7匿名类 http://www.ox520.com/12374.html

下一篇:

已经没有下一篇了!

常见问题

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务