本文实例讲述了php7匿名类的用法。分享给大家供大家参考,具体如下:

<?php
/**
 * created by phpstorm.
 * user: itboot
 * date: 2019/1/17
 * time: 18:15
 */
class an
{
  private $num;
  protected $age = 15;
  public function __construct() {
    $this->num = 1;
  }
  protected function bar(): int {
    return 10;
  }
  public function drive() {
    return new class($this->num) extends an{
      protected $id;
      public function __construct($num) {
        $this->id = $num;
      }
      public function ea() {
        return $this->id + $this->age + $this->bar();
      }
    };
  }
}
echo (new an())->drive()->ea();

<?php
$fun = function (){
  print '这是匿名函数'. php_eol;
};
$fun();
class animal
{
  public $num;
  public function __construct(...$args)
  {
    $this->num = $args[0];
  }
  public function getvalue($su): int
  {
    return $this->num + $su;
  }
}
$an = new animal(4);
echo $an->getvalue(12) . php_eol;
echo '匿名类'. php_eol;
echo (new class(11) extends animal{})->getvalue(12);

更多关于php相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php数组(array)操作技巧大全》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家php程序设计有所帮助。