


Summary of new functions and features of each version of PHP5, new functions and features of php5
Jul 06, 2016 pm 02:24 PMA summary of the new functions and features of each version of PHP5, new functions and new features of php5
Because of PHP’s painful syntax that “gathers the strengths of hundreds of schools of thought”, coupled with the community atmosphere Unfortunately, many people are not interested in new versions and new features. This article will introduce the new features added in PHP5.2 starting from PHP5.6
Directory of this article:
PHP5.2 before: autoload, PDO and MySQLi, type constraints
PHP5.2: JSON support
PHP5.3: deprecated features, anonymous Functions, new magic methods, namespaces, late static binding, Heredoc and Nowdoc, const, ternary operator, Phar
PHP5.4: Short Open Tag, array abbreviation, Traits, built-in web server, details modified
PHP5.5: yield, list() is used for foreach, details modified
PHP5.6: constant enhancement, variable function parameters, namespace enhancement
1. Before PHP5.2 (before 2006)
By the way, let me introduce the features of PHP5.2 that have already appeared but are worth introducing.
autoload
Everyone may know the __autoload() function. If this function is defined, then when an undefined class is used in the code, the function will be called. You can load the corresponding class implementation file in this function, such as:
<span>function</span> __autoload(<span>$classname</span><span>) { </span><span>require_once</span>("{<span>$classname</span>}.php"<span>) }</span><span>/*</span><span> 何問起 hovertree.com </span><span>*/</span>
But this function is no longer recommended because there can only be one such __autoload() function in a project because PHP does not allow functions with duplicate names. But when you use some class libraries, you will inevitably need multiple autoload functions, so spl_autoload_register() replaces it:
?spl_autoload_register(<span>function</span>(<span>$classname</span><span>) { </span><span>require_once</span>("{<span>$classname</span>}.php"<span>) });</span><span>/*</span><span> hovertree.top </span><span>*/</span>
spl_autoload_register() will register a function into the autoload function list. When an undefined class appears, SPL [Note] will call the registered autoload functions one by one in the reverse order of registration, which means you can use spl_autoload_register () Register multiple autoload functions.
Note: SPL: Standard PHP Library, standard PHP library, is designed to solve some classic problems (such as data structure).
PDO and MySQLi
are PHP Data Object, PHP data object, which is PHP’s new database access interface.
According to the traditional style, accessing the MySQL database should look like this:
<span>//</span><span> 連接到服務(wù)器,選擇數(shù)據(jù)庫</span> <span>$conn</span> = <span>mysql_connect</span>("localhost", "user", "password"<span>); </span><span>mysql_select_db</span>("database"<span>); </span><span>//</span><span> 執(zhí)行 SQL 查詢</span> <span>$type</span> = <span>$_POST</span>['type'<span>]; </span><span>$sql</span> = "SELECT * FROM `table` WHERE `type` = {<span>$type</span>}"<span>; </span><span>$result</span> = <span>mysql_query</span>(<span>$sql</span><span>); </span><span>//</span><span> 打印結(jié)果</span> <span>while</span>(<span>$row</span> = <span>mysql_fetch_array</span>(<span>$result</span>,<span> MYSQL_ASSOC)) { </span><span>foreach</span>(<span>$row</span> <span>as</span> <span>$k</span> => <span>$v</span><span>) </span><span>print</span> "{<span>$k</span>}: {<span>$v</span>}\n"<span>; } </span><span>//</span><span> 釋放結(jié)果集,關(guān)閉連接</span> <span>mysql_free_result</span>(<span>$result</span><span>); </span><span>mysql_close</span>(<span>$conn</span><span>); </span><span>/*</span><span> hwq2.com </span><span>*/</span>
In order to make the code database independent, that is, a piece of code is applicable to multiple databases at the same time (for example, the above code is only applicable to MySQL), PHP officially designed PDO.
In addition, PDO also provides more Function, such as:
1. Object-oriented style interface
2. SQL pre-compilation (prepare), placeholder syntax
3. Higher execution efficiency, as an official recommendation, there are special performance optimizations
4 .Supports most SQL databases, no need to change the code when changing the database
The above code implemented with PDO will look like this:
<span>//</span><span> 連接到數(shù)據(jù)庫</span> <span>$conn</span> = <span>new</span> PDO("mysql:host=localhost;dbname=database", "user", "password"<span>); </span><span>//</span><span> 預(yù)編譯SQL, 綁定參數(shù)</span> <span>$query</span> = <span>$conn</span>->prepare("SELECT * FROM `table` WHERE `type` = :type"<span>); </span><span>$query</span>->bindParam("type", <span>$_POST</span>['type'<span>]); </span><span>//</span><span> 執(zhí)行查詢并打印結(jié)果</span> <span>foreach</span>(<span>$query</span>->execute() <span>as</span> <span>$row</span><span>) { </span><span>foreach</span>(<span>$row</span> <span>as</span> <span>$k</span> => <span>$v</span><span>) </span><span>print</span> "{<span>$k</span>}: {<span>$v</span>}\n"<span>; }</span><span>//</span><span> 何問起 hovertree.com</span>
PDO is officially recommended and a more general database access method. If you have no special needs, then you'd better learn and use PDO.
But if you need to use the advanced functions unique to MySQL, then you may You need to try MySQLi, because in order to be able to be used on multiple databases at the same time, PDO does not include those unique features of MySQL.
MySQLi is an enhanced interface for MySQL, providing both process-oriented and object-oriented interfaces. It is also the currently recommended MySQL driver. The old C-style MySQL interface will be closed by default in the future.
Compared with the above two pieces of code, the usage of MySQLi does not have many new concepts. Examples will not be given here. You can refer to the PHP official website documentation [Note].
Note: http://www.php.net/manual/en/mysqli.quickstart.php
Type Constraints
Type constraints can limit the type of parameters, but this mechanism is not perfect and currently only applies to classes, callables (executable types) and arrays (arrays), not Applies to string and int.
<span>//</span><span> 限制第一個參數(shù)為 MyClass, 第二個參數(shù)為可執(zhí)行類型,第三個參數(shù)為數(shù)組</span> <span>function</span> MyFunction(MyClass <span>$a</span>, callable <span>$b</span>, <span>array</span> <span>$c</span><span>) { </span><span>//</span><span> ...</span> }<span>//</span><span> 何問起 hovertree.com</span>
PHP5.2(2006-2011): JSON support
Including json_encode(), json_decode() and other functions. JSON is a very commonly used data exchange format in the Web field and can be directly supported by JS. JSON is actually part of the JS syntax.
JSON series functions can convert array structures in PHP into JSON strings:
<span>$array</span> = ["key" => "value", "array" => [1, 2, 3, 4<span>]]; </span><span>$json</span> = json_encode(<span>$array</span><span>); </span><span>echo</span> "{<span>$json</span>}\n"<span>; </span><span>$object</span> = json_decode(<span>$json</span><span>); </span><span>print_r</span>(<span>$object</span>);<span>/*</span><span> hovertree.top </span><span>*/</span>
Output:
<span>{"key":"value","array":[1,2,3,4]} stdClass Object ( [key] => value [array] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) )</span>
It is worth noting that json_decode() will return an object instead of an array by default. If you need to return an array, you need to set the second parameter to true.
PHP5.3 (2009-2012)
PHP5.3 is a very big update, adding a lot of new features, and also made some modifications that are not backward compatible.
【PHP5.3 deprecated features】: The following features are deprecated. If enabled in the configuration file, PHP will issue a warning at runtime.
Register Globals
This is an option in php.ini (register_globals). When turned on, all form variables ($_GET and $_POST) will be registered as global variables.
See Example below:
<span>if</span><span>(isAuth()) </span><span>$authorized</span> = <span>true</span><span>; </span><span>if</span>(<span>$authorized</span><span>) </span><span>include</span>("page.php");<span>/*</span><span> hovertree.top </span><span>*/</span>
這段代碼在通過驗證時,將 $authorized 設(shè)置為 true. 然后根據(jù) $authorized 的值來決定是否顯示頁面.
但由于并沒有事先把 $authorized 初始化為 false, 當(dāng) register_globals 打開時,可能訪問 /auth.php?authorized=1 來定義該變量值,繞過身份驗證。
該特征屬于歷史遺留問題,在 PHP4.2 中被默認關(guān)閉,在 PHP5.4 中被移除。
Magic Quotes
對應(yīng) php.ini 中的選項 magic_quotes_gpc, 這個特征同樣屬于歷史遺留問題,已經(jīng)在 PHP5.4 中移除。
該特征會將所有用戶輸入進行轉(zhuǎn)義,這看上去不錯,在第一章我們提到過要對用戶輸入進行轉(zhuǎn)義。
但是 PHP 并不知道哪些輸入會進入 SQL , 哪些輸入會進入 Shell, 哪些輸入會被顯示為 HTML, 所以很多時候這種轉(zhuǎn)義會引起混亂。
Safe Mode
很多虛擬主機提供商使用 Safe Mode 來隔離多個用戶,但 Safe Mode 存在諸多問題,例如某些擴展并不按照 Safe Mode 來進行權(quán)限控制。
PHP官方推薦使用操作系統(tǒng)的機制來進行權(quán)限隔離,讓W(xué)eb服務(wù)器以不同的用戶權(quán)限來運行PHP解釋器,請參見第一章中的最小權(quán)限原則.
【PHP5.3的新增、改進】
匿名函數(shù)
也叫閉包(Closures), 經(jīng)常被用來臨時性地創(chuàng)建一個無名函數(shù),用于回調(diào)函數(shù)等用途。
<span>$func</span> = <span>function</span>(<span>$arg</span><span>) { </span><span>print</span> <span>$arg</span><span>; }; </span><span>$func</span>("Hello World! hovertree.top");
以上代碼定義了一個匿名函數(shù),并賦值給了 $func.
可以看到定義匿名函數(shù)依舊使用 function 關(guān)鍵字,只不過省略了函數(shù)名,直接是參數(shù)列表。
然后我們又調(diào)用了 $func 所儲存的匿名函數(shù)。
匿名函數(shù)還可以用 use 關(guān)鍵字來捕捉外部變量:
<span>function</span> arrayPlus(<span>$array</span>, <span>$num</span><span>) { </span><span>array_walk</span>(<span>$array</span>, <span>function</span>(&<span>$v</span>) <span>use</span>(<span>$num</span><span>){ </span><span>$v</span> += <span>$num</span><span>; }); }</span><span>/*</span><span> hovertree.top </span><span>*/</span>
上面的代碼定義了一個 arrayPlus() 函數(shù)(這不是匿名函數(shù)), 它會將一個數(shù)組($array)中的每一項,加上一個指定的數(shù)字($num).
在 arrayPlus() 的實現(xiàn)中,我們使用了 array_walk() 函數(shù),它會為一個數(shù)組的每一項執(zhí)行一個回調(diào)函數(shù),即我們定義的匿名函數(shù)。
在匿名函數(shù)的參數(shù)列表后,我們用 use 關(guān)鍵字將匿名函數(shù)外的 $num 捕捉到了函數(shù)內(nèi),以便知道到底應(yīng)該加上多少。
魔術(shù)方法:__invoke(), __callStatic()
PHP 的面向?qū)ο篌w系中,提供了若干“魔術(shù)方法”,用于實現(xiàn)類似其他語言中的“重載”,如在訪問不存在的屬性、方法時觸發(fā)某個魔術(shù)方法。
隨著匿名函數(shù)的加入,PHP 引入了一個新的魔術(shù)方法 __invoke().
該魔術(shù)方法會在將一個對象作為函數(shù)調(diào)用時被調(diào)用:
<span>class</span><span> A { </span><span>public</span> <span>function</span> __invoke(<span>$str</span><span>) { </span><span>print</span> "A::__invoke(): {<span>$str</span>}"<span>; } } </span><span>$a</span> = <span>new</span><span> A; </span><span>$a</span>("Hello World!何問起");
輸出毫無疑問是:
A::__invoke(): Hello World
__callStatic() 則會在調(diào)用一個不存在的靜態(tài)方法時被調(diào)用。
命名空間
PHP的命名空間有著前無古人后無來者的無比蛋疼的語法:
<?<span>php </span><span>//</span><span> 命名空間的分隔符是反斜杠,該聲明語句必須在文件第一行。 // 命名空間中可以包含任意代碼,但只有 **類, 函數(shù), 常量** 受命名空間影響。</span> <span>namespace XXOO\Test; </span><span>//</span><span> 該類的完整限定名是 \XXOO\Test\A , 其中第一個反斜杠表示全局命名空間。</span> <span>class</span><span> A{} </span><span>//</span><span> 你還可以在已經(jīng)文件中定義第二個命名空間,接下來的代碼將都位于 \Other\Test2 .</span> <span>namespace Other\Test2; </span><span>//</span><span> 實例化來自其他命名空間的對象:</span> <span>$a</span> = <span>new</span><span> \XXOO\Test\A; </span><span>class</span><span> B{} </span><span>//</span><span> 你還可以用花括號定義第三個命名空間</span><span> /*</span><span> 何問起 hovertree.com </span><span>*/</span><span> namespace Other { </span><span>//</span><span> 實例化來自子命名空間的對象:</span> <span>$b</span> = <span>new</span><span> Test2\B; </span><span>//</span><span> 導(dǎo)入來自其他命名空間的名稱,并重命名, // 注意只能導(dǎo)入類,不能用于函數(shù)和常量。</span> <span>use</span> \XXOO\Test\A <span>as</span><span> ClassA } </span>?>
更多有關(guān)命名空間的語法介紹請參見官網(wǎng) [注].
命名空間時常和 autoload 一同使用,用于自動加載類實現(xiàn)文件:
<span>spl_autoload_register( </span><span>function</span> (<span>$class</span><span>) { spl_autoload(</span><span>str_replace</span>("\\", "/", <span>$class</span><span>)); } );</span>
當(dāng)你實例化一個類 \XXOO\Test\A 的時候,這個類的完整限定名會被傳遞給 autoload 函數(shù),autoload 函數(shù)將類名中的命名空間分隔符(反斜杠)替換為斜杠,并包含對應(yīng)文件。
這樣可以實現(xiàn)類定義文件分級儲存,按需自動加載。
注:http://www.php.net/manual/zh/language.namespaces.php
后期靜態(tài)綁定
PHP 的 OPP 機制,具有繼承和類似虛函數(shù)的功能,例如如下的代碼:
<span>class</span><span> A { </span><span>public</span> <span>function</span><span> callFuncXXOO() { </span><span>print</span> <span>$this</span>-><span>funcXXOO(); } </span><span>public</span> <span>function</span><span> funcXXOO() { </span><span>return</span> "A::funcXXOO()"<span>; } } </span><span>class</span> B <span>extends</span><span> A { </span><span>public</span> <span>function</span><span> funcXXOO() { </span><span>return</span> "B::funcXXOO"<span>; } } </span><span>$b</span> = <span>new</span><span> B; </span><span>$b</span>->callFuncXXOO();<span>/*</span><span> 何問起 hovertree.com </span><span>*/</span>
輸出是:
B::funcXXOO
可以看到,當(dāng)在 A 中使用 $this->funcXXOO() 時,體現(xiàn)了“虛函數(shù)”的機制,實際調(diào)用的是 B::funcXXOO().
然而如果將所有函數(shù)都改為靜態(tài)函數(shù):
<span>class</span><span> A { </span><span>static</span> <span>public</span> <span>function</span><span> callFuncXXOO() { </span><span>print</span> self::<span>funcXXOO(); } </span><span>static</span> <span>public</span> <span>function</span><span> funcXXOO() { </span><span>return</span> "A::funcXXOO()"<span>; } } </span><span>class</span> B <span>extends</span><span> A { </span><span>static</span> <span>public</span> <span>function</span><span> funcXXOO() { </span><span>return</span> "B::funcXXOO"<span>; } } </span><span>$b</span> = <span>new</span><span> B; </span><span>$b</span>->callFuncXXOO();
情況就沒這么樂觀了,輸出是:
A::funcXXOO()
這是因為 self 的語義本來就是“當(dāng)前類”,所以 PHP5.3 給 static 關(guān)鍵字賦予了一個新功能:后期靜態(tài)綁定:
<span>class</span><span> A { </span><span>static</span> <span>public</span> <span>function</span><span> callFuncXXOO() { </span><span>print</span> <span>static</span>::<span>funcXXOO(); } </span><span>//</span><span> ...</span> <span>} </span><span>//</span><span> ...</span>
這樣就會像預(yù)期一樣輸出了:
B::funcXXOO
Heredoc 和 Nowdoc
PHP5.3 對 Heredoc 以及 Nowdoc 進行了一些改進,它們都用于在 PHP 代碼中嵌入大段字符串。
Heredoc 的行為類似于一個雙引號字符串:
<span>$name</span> = "MyName"<span>; </span><span>echo</span> <<<<span> TEXT My name is </span>"{<span>$name</span>}".<span> TEXT;</span>
Heredoc 以三個左尖括號開始,后面跟一個標識符(TEXT), 直到一個同樣的頂格的標識符(不能縮進)結(jié)束。
就像雙引號字符串一樣,其中可以嵌入變量。
Heredoc 還可以用于函數(shù)參數(shù),以及類成員初始化:
<span>var_dump</span>(<<<<span>EOD Hello World EOD ); </span><span>class</span><span> A { </span><span>const</span> xx = <<<<span> EOD Hello World EOD; </span><span>public</span> <span>$oo</span> = <<<<span> EOD Hello World EOD; }</span>
Nowdoc 的行為像一個單引號字符串,不能在其中嵌入變量,和 Heredoc 唯一的區(qū)別就是,三個左尖括號后的標識符要以單引號括起來:
<span>$name</span> = "MyName"<span>; </span><span>echo</span> <<< 'TEXT'<span> My name is </span>"{<span>$name</span>}".<span> TEXT;</span>
輸出:
My name is "{<span>$name</span>}".
用 const 定義常量
PHP5.3 起同時支持在全局命名空間和類中使用 const 定義常量。
舊式風(fēng)格:
<span>define</span>("XOOO", "Value");
新式風(fēng)格:
const XXOO = "Value";
const 形式僅適用于常量,不適用于運行時才能求值的表達式:
<span>//</span><span> 正確</span> <span>const</span> XXOO = 1234<span>; </span><span>//</span><span> 錯誤</span> <span>const</span> XXOO = 2 * 617;
三元運算符簡寫形式
舊式風(fēng)格:
<span>echo</span> <span>$a</span> ? <span>$a</span> : "No Value";
可簡寫成:
<span>echo</span> <span>$a</span> ?: "No Value";
即如果省略三元運算符的第二個部分,會默認用第一個部分代替。
Phar
Phar即PHP Archive, 起初只是Pear中的一個庫而已,后來在PHP5.3被重新編寫成C擴展并內(nèi)置到 PHP 中。
Phar用來將多個 .php 腳本打包(也可以打包其他文件)成一個 .phar 的壓縮文件(通常是ZIP格式)。
目的在于模仿 Java 的 .jar, 不對,目的是為了讓發(fā)布PHP應(yīng)用程序更加方便。同時還提供了數(shù)字簽名驗證等功能。
.phar 文件可以像 .php 文件一樣,被PHP引擎解釋執(zhí)行,同時你還可以寫出這樣的代碼來包含(require) .phar 中的代碼:
<span>require</span>("xxoo.phar"<span>); </span><span>require</span>("phar://xxoo.phar/xo/ox.php");
更多信息請參見官網(wǎng) [注].
注:http://www.php.net/manual/zh/phar.using.intro.php
PHP5.4(2012-2013)
Short Open Tag
Short Open Tag 自 PHP5.4 起總是可用。
在這里集中講一下有關(guān) PHP 起止標簽的問題。即:
<?<span>php </span><span>//</span><span> Code... 何問起 hovertree.com</span> ?>
通常就是上面的形式,除此之外還有一種簡寫形式:
<? <span>/*</span><span> Code... </span><span>*/</span> ?>
還可以把
<?php <span>echo</span> <span>$xxoo</span>;?>
簡寫成:
<?= <span>$xxoo</span>;?>
這種簡寫形式被稱為 Short Open Tag, 在 PHP5.3 起被默認開啟,在 PHP5.4 起總是可用。
使用這種簡寫形式在 HTML 中嵌入 PHP 變量將會非常方便。
對于純 PHP 文件(如類實現(xiàn)文件), PHP 官方建議頂格寫起始標記,同時 省略 結(jié)束標記。
這樣可以確保整個 PHP 文件都是 PHP 代碼,沒有任何輸出,否則當(dāng)你包含該文件后,設(shè)置 Header 和 Cookie 時會遇到一些麻煩 [注].
注:Header 和 Cookie 必須在輸出任何內(nèi)容之前被發(fā)送。
數(shù)組簡寫形式
這是非常方便的一項特征!
<span>//</span><span> 原來的數(shù)組寫法</span> <span>$arr</span> = <span>array</span>("key" => "value", "key2" => "value2"<span>); </span><span>//</span><span> 簡寫形式</span> <span>$arr</span> = ["key" => "value", "key2" => "value2"<span>]; </span><span>//</span><span> 何問起 hovertree.com</span>
Traits
所謂Traits就是“構(gòu)件”,是用來替代繼承的一種機制。PHP中無法進行多重繼承,但一個類可以包含多個Traits.
<span>//</span><span> Traits不能被單獨實例化,只能被類所包含</span> <span>trait SayWorld { </span><span>public</span> <span>function</span><span> sayHello() { </span><span>echo</span> 'World!'<span>; } } </span><span>class</span><span> MyHelloWorld { </span><span>//</span><span> 將SayWorld中的成員包含進來</span> <span>use</span><span> SayWorld; } </span><span>$xxoo</span> = <span>new</span><span> MyHelloWorld(); </span><span>//</span><span> sayHello() 函數(shù)是來自 SayWorld 構(gòu)件的</span> <span>$xxoo</span>->sayHello();
Traits還有很多神奇的功能,比如包含多個Traits, 解決沖突,修改訪問權(quán)限,為函數(shù)設(shè)置別名等等。
Traits中也同樣可以包含Traits. 篇幅有限不能逐個舉例,詳情參見官網(wǎng) [注].
注:http://www.php.net/manual/zh/language.oop5.traits.php
內(nèi)置 Web 服務(wù)器
PHP從5.4開始內(nèi)置一個輕量級的Web服務(wù)器,不支持并發(fā),定位是用于開發(fā)和調(diào)試環(huán)境。
在開發(fā)環(huán)境使用它的確非常方便。
php -S localhost:8000
這樣就在當(dāng)前目錄建立起了一個Web服務(wù)器,你可以通過 http://localhost:8000/ 來訪問。
其中l(wèi)ocalhost是監(jiān)聽的ip,8000是監(jiān)聽的端口,可以自行修改。
很多應(yīng)用中,都會進行URL重寫,所以PHP提供了一個設(shè)置路由腳本的功能:
php -S localhost:8000 index.php
這樣一來,所有的請求都會由index.php來處理。
你還可以使用 XDebug 來進行斷點調(diào)試。
細節(jié)修改
PHP5.4 新增了動態(tài)訪問靜態(tài)方法的方式:
<span>$func</span> = "funcXXOO"<span>; A</span>::{<span>$func</span>}();
新增在實例化時訪問類成員的特征:
(<span>new</span> MyClass)->xxoo();
新增支持對函數(shù)返回數(shù)組的成員訪問解析(這種寫法在之前版本是會報錯的):
<span>print</span> func()[0];
PHP5.5(2013起)
yield
yield關(guān)鍵字用于當(dāng)函數(shù)需要返回一個迭代器的時候, 逐個返回值。
<span>function</span><span> number10() { </span><span>for</span>(<span>$i</span> = 1; <span>$i</span> <= 10; <span>$i</span> += 1<span>) yield </span><span>$i</span><span>; }</span>
該函數(shù)的返回值是一個數(shù)組:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list() 用于 foreach
可以用 list() 在 foreach 中解析嵌套的數(shù)組:
<span>$array</span> =<span> [ [</span>1, 2, 3],<span> [</span>4, 5, 6],<span> ]; </span><span>foreach</span> (<span>$array</span> <span>as</span> <span>list</span>(<span>$a</span>, <span>$b</span>, <span>$c</span><span>)) </span><span>echo</span> "{<span>$a</span>} {<span>$b</span>} {<span>$c</span>}\n";
結(jié)果:
1 2 3 4 5 6
細節(jié)修改
不推薦使用 mysql 函數(shù),推薦使用 PDO 或 MySQLi, 參見前文。
不再支持Windows XP.
可用 MyClass::class 取到一個類的完整限定名(包括命名空間)。
empty() 支持表達式作為參數(shù)。
try-catch 結(jié)構(gòu)新增 finally 塊。
PHP5.6
更好的常量
定義常量時允許使用之前定義的常量進行計算:
<span>const</span> A = 2<span>; </span><span>const</span> B = A + 1<span>; </span><span>class</span><span> C { </span><span>const</span> STR = "hello"<span>; </span><span>const</span> STR2 = self::STR + ", world"<span>; }</span>
允許常量作為函數(shù)參數(shù)默認值:
<span>function</span> func(<span>$arg</span> = C::STR2)
更好的可變函數(shù)參數(shù)
用于代替 func_get_args()
<span>function</span> add(...<span>$args</span><span>) { </span><span>$result</span> = 0<span>; </span><span>foreach</span>(<span>$args</span> <span>as</span> <span>$arg</span><span>) </span><span>$result</span> += <span>$arg</span><span>; </span><span>return</span> <span>$result</span><span>; }</span><span>//</span><span> 何問起 hovertree.com</span>
同時可以在調(diào)用函數(shù)時,把數(shù)組展開為函數(shù)參數(shù):// 結(jié)果為 6
命名空間
命名空間支持常量和函數(shù):
<span>namespace Name\Space { </span><span>const</span> FOO = 42<span>; </span><span>function</span> f() { <span>echo</span> <span>__FUNCTION__</span>."\n"<span>; } } namespace { </span><span>use</span> <span>const</span><span> Name\Space\FOO; </span><span>use</span> <span>function</span><span> Name\Space\f; </span><span>echo</span> FOO."\n"<span>; f(); }</span>
推薦:http://www.cnblogs.com/roucheng/p/3454913.html

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Early this morning, Apple officially released iPadOS18. This system not only has the classic functions of iOS18, but also adds some unique functions, such as supporting mathematical note calculators, etc., which further improves the experience of iPad users. Friends who are interested Come and take a look. This time iPadOS18 not only perfectly inherits the core functions of iOS18, such as the personalized control center design, which allows users to freely adjust the order and layout of control items according to personal preferences, and the highly anticipated game mode, providing gamers with smoother and more The immersive gaming experience also incorporates a number of unique features specifically targeting the iPad’s large screen advantages and the creative uses of Apple Pencil, further expanding the iPad’s productivity.

Regarding Llama3, new test results have been released - the large model evaluation community LMSYS released a large model ranking list. Llama3 ranked fifth, and tied for first place with GPT-4 in the English category. The picture is different from other benchmarks. This list is based on one-on-one battles between models, and the evaluators from all over the network make their own propositions and scores. In the end, Llama3 ranked fifth on the list, followed by three different versions of GPT-4 and Claude3 Super Cup Opus. In the English single list, Llama3 overtook Claude and tied with GPT-4. Regarding this result, Meta’s chief scientist LeCun was very happy and forwarded the tweet and

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

How to easily check the installed version of Oracle requires specific code examples. As a software widely used in enterprise-level database management systems, the Oracle database has many versions and different installation methods. In our daily work, we often need to check the installed version of the Oracle database for corresponding operations and maintenance. This article will introduce how to easily check the installed version of Oracle and give specific code examples. Method 1: Through SQL query in the Oracle database, we can

Summary of the system() function under Linux In the Linux system, the system() function is a very commonly used function, which can be used to execute command line commands. This article will introduce the system() function in detail and provide some specific code examples. 1. Basic usage of the system() function. The declaration of the system() function is as follows: intsystem(constchar*command); where the command parameter is a character.

The meaning and difference of PHP version NTS PHP is a popular server-side scripting language that is widely used in the field of web development. There are two main versions of PHP: ThreadSafe(TS) and Non-ThreadSafe(NTS). On the official website of PHP, we can see two different PHP download versions, namely PHPNTS and PHPTS. So, what does PHP version NTS mean? What is the difference between it and the TS version? Next,

1. Open the Douyin app and click [Me] in the lower right corner to enter the personal page. 2. Click the [Three Stripes] icon in the upper right corner and select the [Settings] option in the pop-up menu bar. 3. In the settings page, scroll to the bottom to view the current version number information of Douyin.

The editor recently learned that the new feature of Microsoft Edge browser "Super Drag" has been launched, unlocking the fourth way to open links in new tabs, making it easier for users to open links faster. Currently, in the Microsoft Edge browser, if users want to open a link or image in a new tab, there are three ways: 1. Right-click the link or image, and then select the corresponding operation option. 2. Drag the link or image to the tab bar. 3. Use the mouse wheel to click on the link or image. "Super Drag" brings a fourth kind of interaction, where users click on a link, part of text, or image and then drag it sideways, up, or down a little to open it in a new tab. After the user drags the text, the default search engine of the Edge browser will be called by default and a new tab will be opened.
