本篇文章给大家带来的内容是关于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);以上就是PHP7匿名类的用法示例(代码)的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号