PHP7匿名类的用法示例(代码)
来源:不言
发布时间:2019-03-23 15:16:01
阅读量:1161
本篇文章给大家带来的内容是关于PHP7匿名类的用法示例(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php
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();
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?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);
|