<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 新的特性 ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#標量類型聲明)標量類型聲明 標量類型聲明有兩種模式:強制(默認)模式、嚴格模式。下列參數類型可以使用(無論用強制模式還是嚴格模式):字符串(string)、整形(int)、浮點數(float)和布爾型(bool)。其他類型在 PHP5 中有支持:類名、接口、數組和可被調用的。 ~~~ <?php // Coercive mode function sumOfInts(int ...$ints) { return array_sum($ints); } var_dump(sumOfInts(2, '3', 4.1)); ~~~ 上述例子輸出: ~~~ int(9) ~~~ 當開啟嚴格模式后,一個?[declare](http://php.net/manual/en/control-structures.declare.php)?聲明必須置于 PHP 腳本文件開頭,這意味著嚴格聲明標量是基于文件可配的。這個指令不僅影響參數的類型聲明,也影響到函數的返回值聲明(詳見下面的返回值聲明)。 詳細的標量類型聲明的文檔與示例,可以查看[類型聲明](http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration)頁面。 ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#返回類型聲明)返回類型聲明 PHP7 新增了返回類型聲明,類似于參數類型聲明,返回類型聲明提前聲明了函數返回值的類型。可用的聲明類型與參數聲明中可用的類型相同。 ~~~ <?php function arraysSum(array ...$arrays): array { return array_map(function(array $array): int { return array_sum($array); }, $arrays); } print_r(arraysSum([1,2,3], [4,5,6], [7,8,9])); ~~~ 上述代碼返回值為: ~~~ Array ( [0] => 6 [1] => 15 [2] => 24 ) ~~~ 詳細的返回值聲明相關的文檔和示例代碼可以查閱[返回值聲明](http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration)文檔。 ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#null合并算子操作符)NULL合并算子(操作符“??”) 空合并算子的操作符為?`??`?,已經作為一種語法糖用于日常需求中用于三元表達式,它與 isset() 同時發生。如果變量存在且不為空,它就會返回對應的值,相反,它返回它的第二個操作數。 ~~~ <?php // Fetches the value of $_GET['user'] and returns 'nobody' // if it does not exist. $username = $_GET['user'] ?? 'nobody'; // This is equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; // Coalesces can be chained: this will return the first // defined value out of $_GET['user'], $_POST['user'], and // 'nobody'. $username = $_GET['user'] ?? $_POST['user'] ?? 'nobody'; ?> ~~~ ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#太空船操作符組合比較符rfc)太空船操作符(組合比較符,[RFC](https://wiki.php.net/rfc/combined-comparison-operator)) 太空船操作符用于比較兩個表達式。它返回一個大于 0、等于 0、小于 0 的數,用于表示 $a 與 $b 之間的關系。比較的原則是沿用 PHP 的[常規比較規則](http://php.net/manual/en/types.comparisons.php)進行的。 ~~~ <?php // Integers echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1 // Floats echo 1.5 <=> 1.5; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1 // Strings echo "a" <=> "a"; // 0 echo "a" <=> "b"; // -1 echo "b" <=> "a"; // 1 ?> ~~~ ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#通過-define-定義常量數組)通過 define() 定義常量數組 Array 類型的常量可以通過 define() 來定義。在 PHP5.6 中僅能通過 const 定義。 ~~~ <?php define('ANIMALS', [ 'dog', 'cat', 'bird' ]); echo ANIMALS[1]; // outputs "cat" ?> ~~~ ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#匿名類)匿名類 可以通過 new 關鍵字初始化一個匿名類。匿名類使用場景與完整的類場景相同。 ~~~ <?php interface Logger { public function log(string $msg); } class Application { private $logger; public function getLogger(): Logger { return $this->logger; } public function setLogger(Logger $logger) { $this->logger = $logger; } } $app = new Application; $app->setLogger(new class implements Logger { public function log(string $msg) { echo $msg; } }); var_dump($app->getLogger()); ?> ~~~ 上面代碼輸出: ~~~ object(class@anonymous)#2 (0) { } ~~~ 詳細文檔可以參考[匿名類文檔](http://php.net/manual/en/language.oop5.anonymous.php) ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#unicode-codepoint-轉譯語法)Unicode codepoint 轉譯語法 通過十六進制內容與雙引號組成的字符串生成 Unicode codepoint,可以接受任何有效的 codepoint,并且開頭的 0 是可以省略的。 ~~~ echo "\u{aa}"; echo "\u{0000aa}"; echo "\u{9999}"; ~~~ 上面代碼輸出: ~~~ (same as before but with optional leading 0's) ~~~ ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#closurecall)[Closure::call()](http://php.net/manual/en/closure.call.php) 閉包?[Closure::call()](http://php.net/manual/en/closure.call.php)?有著更好的性能,簡短干練的暫時綁定一個方法到對象上閉包并調用它。 ~~~ <?php class A {private $x = 1;} // Pre PHP 7 code $getXCB = function() {return $this->x;}; $getX = $getXCB->bindTo(new A, 'A'); // intermediate closure echo $getX(); // PHP 7+ code $getX = function() {return $this->x;}; echo $getX->call(new A); ~~~ 上述代碼輸出: ~~~ 1 1 ~~~ ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#為-unserialize-提供過濾)為 unserialize() 提供過濾 這個特性意在提供更安全的方式解包不可靠的數據。通過白名單的方式來防止代碼注入。 ~~~ <?php // converts all objects into __PHP_Incomplete_Class object $data = unserialize($foo, ["allowed_classes" => false]); // converts all objects into __PHP_Incomplete_Class object except those of MyClass and MyClass2 $data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]); // default behaviour (same as omitting the second argument) that accepts all classes $data = unserialize($foo, ["allowed_classes" => true]); ~~~ ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#intlchar)[IntlChar](http://php.net/manual/en/class.intlchar.php) 新增加的 IntlChar 類意在于暴露出更多的 ICU 功能。類自身定義了許多靜態方法用于操作 unicode 字符。 ~~~ <?php printf('%x', IntlChar::CODEPOINT_MAX); echo IntlChar::charName('@'); var_dump(IntlChar::ispunct('!')); ~~~ 上述代碼輸出: ~~~ 10ffff COMMERCIAL AT bool(true) ~~~ 若要使用此類,請先安裝Intl擴展。 ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#預期-增強的斷言)預期 (增強的斷言) 預期(增強的斷言)是向后兼用以增強 assert() 方法。在代碼中啟用斷言為零成本,并且提供拋出特定異常的能力。 在使用老版本 API 時,如果第一個參數是一個字符串,那么它將被解析。第二個參數可以是一個簡單的字符串(導致 AssertionError 被觸發),或一個包含一個錯誤消息的自定義異常對象。 ~~~ <?php ini_set('assert.exception', 1); class CustomError extends AssertionError {} assert(false, new CustomError('Some error message')); ~~~ 上述代碼輸出: ~~~ Fatal error: Uncaught CustomError: Some error message ~~~ 這個特性會帶來兩個 PHP.ini 設置(以及它們的默認值): * zend.assertions = 1 * assert.exception = 0 zend.assertions 有三種值: * 1 = 生成并且執行代碼(開發模式) * 0 = 執行代碼并且在運行期間跳來跳去 * -1 = 不生成任何代碼 (0開銷, 生產模式) assert.exception 意味著斷言失敗時拋出異常。默認關閉保持兼容舊的 assert() 函數。 ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#使用-use-集體聲明)使用 use 集體聲明 在 PHP7 之前需要聲明一大堆命名空間,但是現在可以通過 use 的新特性,批量聲明。 ~~~ <?php // Pre PHP 7 code use some\namespace\ClassA; use some\namespace\ClassB; use some\namespace\ClassC as C; use function some\namespace\fn_a; use function some\namespace\fn_b; use function some\namespace\fn_c; use const some\namespace\ConstA; use const some\namespace\ConstB; use const some\namespace\ConstC; // PHP 7+ code use some\namespace\{ClassA, ClassB, ClassC as C}; use function some\namespace\{fn_a, fn_b, fn_c}; use const some\namespace\{ConstA, ConstB, ConstC}; ~~~ ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#generator-return-expressions)Generator Return Expressions This feature builds upon the generator functionality introduced into PHP 5.5\. It enables for a return statement to be used within a generator to enable for a final expression to be returned (return by reference is not allowed). This value can be fetched using the new Generator::getReturn() method, which may only be used once the generator has finishing yielding values. ~~~ <?php $gen = (function() { yield 1; yield 2; return 3; })(); foreach ($gen as $val) { echo $val, PHP_EOL; } echo $gen->getReturn(), PHP_EOL; ~~~ 上述代碼輸出: ~~~ 1 2 3 ~~~ Being able to explicitly return a final value from a generator is a handy ability to have. This is because it enables for a final value to be returned by a generator (from perhaps some form of coroutine computation) that can be specifically handled by the client code executing the generator. This is far simpler than forcing the client code to firstly check whether the final value has been yielded, and then if so, to handle that value specifically. ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#generator-delegation)Generator Delegation Generator delegation builds upon the ability of being able to return expressions from generators. It does this by using an new syntax of yield from , where can be any Traversable object or array. This will be advanced until no longer valid, and then execution will continue in the calling generator. This feature enables yield statements to be broken down into smaller operations, thereby promoting cleaner code that has greater reusability. ~~~ <?php function gen() { yield 1; yield 2; return yield from gen2(); } function gen2() { yield 3; return 4; } $gen = gen(); foreach ($gen as $val) { echo $val, PHP_EOL; } echo $gen->getReturn(); ~~~ 上述代碼輸出: ~~~ 1 2 3 4 ~~~ ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#通過-intdiv-做整除)通過 intdiv() 做整除 intdiv() 函數來處理整除,并返回一個整數。 ~~~ <?php var_dump(intdiv(10, 3)); ~~~ 上述代碼輸出: ~~~ int(3) ~~~ ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#session_start-選項)session_start() 選項 該特性給 session_start() 函數提供一些設置能力,當然這些設置可以在 PHP.ini 中設置。 ~~~ <?php session_start(['cache_limiter' => 'private']); // sets the session.cache_limiter option to private ~~~ 這個特性還引入了一個新的 php.ini 設置( session.lazy_write ),默認情況下為 true,表示改變會話數據只是重寫。 ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#preg_replace_callback_array-函數)preg_replace_callback_array() 函數 這個新功能,當你使用 preg_replace_callback() 函數時代碼更清晰可讀。在PHP7之前,每個正則表達式都需要回調( preg_replace_callback() 函數的第二個參數 )中來實現功能,這會使流程混亂不可控。 現在,回調可以通過與正則表達式綁定著寫,只需將正則表達式作為 key,回調函數作為 value。 ~~~ <?php $tokenStream = []; // [tokenName, lexeme] pairs $input = <<<'end' $a = 3; // variable initialisation end; // Pre PHP 7 code preg_replace_callback( [ '~\$[a-z_][a-z\d_]*~i', '~=~', '~[\d]+~', '~;~', '~//.*~' ], function ($match) use (&$tokenStream) { if (strpos($match[0], '$') === 0) { $tokenStream[] = ['T_VARIABLE', $match[0]]; } elseif (strpos($match[0], '=') === 0) { $tokenStream[] = ['T_ASSIGN', $match[0]]; } elseif (ctype_digit($match[0])) { $tokenStream[] = ['T_NUM', $match[0]]; } elseif (strpos($match[0], ';') === 0) { $tokenStream[] = ['T_TERMINATE_STMT', $match[0]]; } elseif (strpos($match[0], '//') === 0) { $tokenStream[] = ['T_COMMENT', $match[0]]; } }, $input ); // PHP 7+ code preg_replace_callback_array( [ '~\$[a-z_][a-z\d_]*~i' => function ($match) use (&$tokenStream) { $tokenStream[] = ['T_VARIABLE', $match[0]]; }, '~=~' => function ($match) use (&$tokenStream) { $tokenStream[] = ['T_ASSIGN', $match[0]]; }, '~[\d]+~' => function ($match) use (&$tokenStream) { $tokenStream[] = ['T_NUM', $match[0]]; }, '~;~' => function ($match) use (&$tokenStream) { $tokenStream[] = ['T_TERMINATE_STMT', $match[0]]; }, '~//.*~' => function ($match) use (&$tokenStream) { $tokenStream[] = ['T_COMMENT', $match[0]]; } ], $input ); ~~~ ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#csprng-系列函數)[CSPRNG](http://php.net/manual/en/book.csprng.php)?系列函數 該特性涵蓋兩個函數,用于生成安全的整形與字符串,主要用于密碼場景。它提供了簡單的 API 和平臺無關性。 ~~~ string random_bytes(int length); int random_int(int min, int max); ~~~ 兩個函數在沒有足夠的隨機性時會報 E_WARNING 錯誤并且返回 false。 ### [](https://github.com/pangee/Migrating-from-PHP5.6.x-to-PHP7.0.x/blob/master/New-features.md#list-可以打開對象實現-arrayaccess)list() 可以打開對象實現?[ArrayAccess](http://php.net/manual/en/class.arrayaccess.php)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看