PHP7匿名類的用法示例
本文實例講述了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 '這是匿名函數(shù)'. 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);
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php中配置文件操作 如config.php文件的讀取修改等操作
對形如config.php文件的讀取,修改等操作的代碼,需要的朋友可以參考下2012-07-07
php設(shè)置靜態(tài)內(nèi)容緩存時間的方法
這篇文章主要介紹了php設(shè)置靜態(tài)內(nèi)容緩存時間的方法,涉及針對header函數(shù)中參數(shù)的應(yīng)用技巧,非常具有實用價值,需要的朋友可以參考下2014-12-12
PHP正則刪除html代碼中a標簽并保留標簽內(nèi)容的方法
這篇文章主要介紹了PHP正則刪除html代碼中a標簽并保留標簽內(nèi)容的方法,涉及php基于正則的字符串匹配與子表達式操作相關(guān)技巧,需要的朋友可以參考下2017-05-05
在php7中MongoDB實現(xiàn)模糊查詢的方法詳解
MongoDB模糊查詢語句相信對大家來說都不陌生,這篇文章主要給大家介紹了在php 7中MongoDB實現(xiàn)模糊查詢的方法,文中給出了詳細的介紹和示例代碼,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友一起來看看吧。2017-05-05

