PHP7匿名類的用法示例
本文實(shí)例講述了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è)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP中使用file_get_contents抓取網(wǎng)頁中文亂碼問題解決方法
這篇文章主要介紹了PHP中使用file_get_contents抓取網(wǎng)頁中文亂碼問題解決方法,可以通過使用curl配置gzip選項(xiàng)來解決,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12linux php mysql數(shù)據(jù)庫備份實(shí)現(xiàn)代碼
想在PHP后臺(tái)管理直接能夠備份數(shù)據(jù)庫,于是想呀想,一直沒有什么思路,一開始是考慮用php來訪問服務(wù)器安裝mysql的目錄,比如 /usr/local/mysql/data目錄,直接把下面對(duì)應(yīng)的文件進(jìn)行備份2009-03-03php命令行(cli)模式下報(bào)require 加載路徑錯(cuò)誤的解決方法
本文給大家解決的是在php的cli模式下做任務(wù)計(jì)劃的php腳本總是執(zhí)行不成功,報(bào)“require 加載路徑錯(cuò)誤”,后來經(jīng)過一番研究,才找到問題所在,這里分享給大家。2015-11-11關(guān)于zend studio 出現(xiàn)亂碼問題的總結(jié)
本篇文章是對(duì)zend studio出現(xiàn)亂碼問題進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友參考下2013-06-06php簡單實(shí)現(xiàn)查詢數(shù)據(jù)庫返回json數(shù)據(jù)
這篇文章主要介紹了php簡單實(shí)現(xiàn)查詢數(shù)據(jù)庫返回json數(shù)據(jù),并附上2則示例代碼,非常的簡單實(shí)用,有需要的小伙伴可以參考下。2015-04-04