php7 匿名继承类_PHP7匿名类的用法示例
本文實例講述了PHP7匿名類的用法。分享給大家供大家參考,具體如下:
/**
* 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();
$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程序設計有所幫助。
總結
以上是生活随笔為你收集整理的php7 匿名继承类_PHP7匿名类的用法示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: byte数组穿换成pcm格式_形象地介绍
- 下一篇: HiccDS共享音乐列表