php 7.0 部分知识点整理
2016-11-20 01:20
239 查看
部分向后不兼容的变更
$HTTP_RAW_POST_DATA 被移除
不再提供 $HTTP_RAW_POST_DATA 变量。 请使用 php://input 作为替代。
INI 文件中 # 注释格式被移除
在 INI 文件中,不再支持以 # 开始的注释行, 请使用 ;(分号)来表示注释。 此变更适用于 php.ini 以及用 parse_ini_file() 和 parse_ini_string() 函数来处理的文件。
foreach的变化
foreach发生了细微的变化,控制结构, 主要围绕阵列的内部数组指针和迭代处理的修改。
foreach不再改变内部数组指针
在PHP7之前,当数组通过 foreach 迭代时,数组指针会移动。现在开始,不再如此,见下面代码
foreach 通过值遍历时,操作的值为数组的副本
当默认使用通过值遍历数组时,foreach 实际操作的是数组的迭代副本,而非数组本身。这就意味着,foreach 中的操作不会修改原数组的值。
foreach通过引用遍历时,有更好的迭代特性
当使用引用遍历数组时,现在 foreach 在迭代中能更好的跟踪变化。例如,在迭代中添加一个迭代值到数组中,参考下面的代码:
=========================================
转载请注明出处:http://blog.csdn.net/m0sh1 http://www.share345.com
部分新特性:
返回值类型声明 (例如 :a array) 和 参数 类型声明
以上例程会输出:
null 合并运算符 ?? (两个问号)
太空船操作符用于比较两个表达式。当a小于、等于或大于b时它分别返回-1、0或1。 比较的原则是沿用 PHP 的常规比较规则进行的。
通过 define() 定义常量数组
Array 类型的常量现在可以通过 definedefine() 来定义。在 PHP5.6 中仅能通过 const 定义。
匿名类
现在支持通过new class 来实例化一个匿名类,这可以用来替代一些“用后即焚”的完整类定义。
http://php.net/manual/zh/language.oop5.anonymous.php
以上例程会输出:
Unicode codepoint 转译语法
这接受一个以16进制形式的 Unicode codepoint,并打印出一个双引号或heredoc包围的 UTF-8 编码格式的字符串。 可以接受任何有效的 codepoint,并且开头的 0 是可以省略的。
以上例程会输出:
Closure::call()
Closure::call() 现在有着更好的性能,简短干练的暂时绑定一个方法到对象上闭包并调用它。
为unserialize()提供过滤
这个特性旨在提供更安全的方式解包不可靠的数据。它通过白名单的方式来防止潜在的代码注入。
Group use declarations
从同一 namespace 导入的类、函数和常量现在可以通过单个 use 语句 一次性导入了。
Session options
session_start() now accepts an array of options that override the session configuration directives normally set in php.ini.
These options have also been expanded to support session.lazy_write, which is on by default and causes PHP to only overwrite any session file if the session data has changed, and read_and_close, which is an option that can only be passed to session_start() to indicate that the session data should be read and then the session should immediately be closed unchanged.
For example, to set session.cache_limiter to private and immediately close the session after reading it:
php 7 函数变量声明
范例
Basic class type declaration
以上例程会输出:
Nullable type declaration
以上例程会输出:
Strict typing
It is possible to enable strict mode on a per-file basis. In strict mode, only a variable of exact type of the type declaration will be accepted, or a TypeError will be thrown. The only exception to this rule is that an integer may be given to a function expecting a float.
以上例程会输出:
Catching TypeError
可变数量的参数列表
PHP 在用户自定义函数中支持可变数量的参数列表。在 PHP 5.6 及以上的版本中,由 … 语法实现;在 PHP 5.5 及更早版本中,使用函数 func_num_args(),func_get_arg(),和 func_get_args() 。
… in PHP 5.6+
以上例程会输出:
返回值 类型声明
Basic return type declaration
以上例程会输出:
有 declare(strict_types=1); 声明 那么返回值 也必须做声明
php 7.0 移除了 mysql 扩展 链接 mysql 使用php_pdo_mysql或php_mysqli
$HTTP_RAW_POST_DATA 被移除
不再提供 $HTTP_RAW_POST_DATA 变量。 请使用 php://input 作为替代。
INI 文件中 # 注释格式被移除
在 INI 文件中,不再支持以 # 开始的注释行, 请使用 ;(分号)来表示注释。 此变更适用于 php.ini 以及用 parse_ini_file() 和 parse_ini_string() 函数来处理的文件。
foreach的变化
foreach发生了细微的变化,控制结构, 主要围绕阵列的内部数组指针和迭代处理的修改。
foreach不再改变内部数组指针
在PHP7之前,当数组通过 foreach 迭代时,数组指针会移动。现在开始,不再如此,见下面代码
<?php $array = [0, 1, 2]; foreach ($array as &$val) { var_dump(current($array)); } ?> Output of the above example in PHP 5: int(1) int(2) bool(false) Output of the above example in PHP 7: int(0) int(0) int(0)
foreach 通过值遍历时,操作的值为数组的副本
当默认使用通过值遍历数组时,foreach 实际操作的是数组的迭代副本,而非数组本身。这就意味着,foreach 中的操作不会修改原数组的值。
foreach通过引用遍历时,有更好的迭代特性
当使用引用遍历数组时,现在 foreach 在迭代中能更好的跟踪变化。例如,在迭代中添加一个迭代值到数组中,参考下面的代码:
<?php $array = [0]; foreach ($array as &$val) { var_dump($val); $array[1] = 1; } ?> Output of the above example in PHP 5: int(0) Output of the above example in PHP 7: int(0) int(1)
=========================================
转载请注明出处:http://blog.csdn.net/m0sh1 http://www.share345.com
部分新特性:
返回值类型声明 (例如 :a array) 和 参数 类型声明
<?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 )
null 合并运算符 ?? (两个问号)
<?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'; ?>
太空船操作符用于比较两个表达式。当a小于、等于或大于b时它分别返回-1、0或1。 比较的原则是沿用 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 ?>
通过 define() 定义常量数组
Array 类型的常量现在可以通过 definedefine() 来定义。在 PHP5.6 中仅能通过 const 定义。
<?php define('ANIMALS', [ 'dog', 'cat', 'bird' ]); echo ANIMALS[1]; // outputs "cat" ?>
匿名类
现在支持通过new class 来实例化一个匿名类,这可以用来替代一些“用后即焚”的完整类定义。
http://php.net/manual/zh/language.oop5.anonymous.php
<?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) { }
Unicode codepoint 转译语法
这接受一个以16进制形式的 Unicode codepoint,并打印出一个双引号或heredoc包围的 UTF-8 编码格式的字符串。 可以接受任何有效的 codepoint,并且开头的 0 是可以省略的。
echo "\u{aa}"; echo "\u{0000aa}"; echo "\u{9999}";
以上例程会输出:
ª ª (same as before but with optional leading 0's) 香
Closure::call()
Closure::call() 现在有着更好的性能,简短干练的暂时绑定一个方法到对象上闭包并调用它。
<?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);
为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]);
Group use declarations
从同一 namespace 导入的类、函数和常量现在可以通过单个 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}; ?>
Session options
session_start() now accepts an array of options that override the session configuration directives normally set in php.ini.
These options have also been expanded to support session.lazy_write, which is on by default and causes PHP to only overwrite any session file if the session data has changed, and read_and_close, which is an option that can only be passed to session_start() to indicate that the session data should be read and then the session should immediately be closed unchanged.
For example, to set session.cache_limiter to private and immediately close the session after reading it:
<?php session_start([ 'cache_limiter' => 'private', 'read_and_close' => true, ]); ?>
php 7 函数变量声明
范例
Basic class type declaration
<?php class C {} class D extends C {} // This doesn't extend C. class E {} function f(C $c) { echo get_class($c)."\n"; } f(new C); f(new D); f(new E); ?>
以上例程会输出:
C D Fatal error: Uncaught TypeError: Argument 1 passed to f() must be an instance of C, instance of E given, called in - on line 14 and defined in -:8 Stack trace: #0 -(14): f(Object(E)) #1 {main} thrown in - on line 8
Nullable type declaration
<?php class C {} function f(C $c = null) { var_dump($c); } f(new C); f(null); ?>
以上例程会输出:
object(C)#1 (0) { } NULL
Strict typing
It is possible to enable strict mode on a per-file basis. In strict mode, only a variable of exact type of the type declaration will be accepted, or a TypeError will be thrown. The only exception to this rule is that an integer may be given to a function expecting a float.
<?php declare(strict_types=1); function sum(int $a, int $b) { return $a + $b; } var_dump(sum(1, 2)); var_dump(sum(1.5, 2.5)); ?>
以上例程会输出:
int(3) Fatal error: Uncaught TypeError: Argument 1 passed to sum() must be of the type integer, float given, called in - on line 9 and defined in -:4 Stack trace: #0 -(9): sum(1.5, 2.5) #1 {main} thrown in - on line 4
Catching TypeError
<?php declare(strict_types=1); function sum(int $a, int $b) { return $a + $b; } try { var_dump(sum(1, 2)); var_dump(sum(1.5, 2.5)); } catch (TypeError $e) { echo 'Error: '.$e->getMessage(); } ?>
可变数量的参数列表
PHP 在用户自定义函数中支持可变数量的参数列表。在 PHP 5.6 及以上的版本中,由 … 语法实现;在 PHP 5.5 及更早版本中,使用函数 func_num_args(),func_get_arg(),和 func_get_args() 。
… in PHP 5.6+
<?php function sum(...$numbers) { $acc = 0; foreach ($numbers as $n) { $acc += $n; } return $acc; } echo sum(1, 2, 3, 4); ?>
以上例程会输出:
10
返回值 类型声明
Basic return type declaration
<?php function sum($a, $b): float { return $a + $b; } // Note that a float will be returned. var_dump(sum(1, 2)); ?>
以上例程会输出:
float(3)
有 declare(strict_types=1); 声明 那么返回值 也必须做声明
php 7.0 移除了 mysql 扩展 链接 mysql 使用php_pdo_mysql或php_mysqli
相关文章推荐
- php 小数整理,向上取整,向下去整,四舍五入,丢弃小数部分,保留整数部分
- 黑马程序员——多线程第一部分整理(简要知识点笔记)
- 【旧代码整理】代码说明 /project/init.php 部分
- PHP中面相对象对象的知识点整理
- 我整理的PHP 7.0主要新特性
- php中关于mysqli和mysql区别的一些知识点整理
- PHP 易错知识点整理
- 本周笔记整理(关于php面向对象的一些基层知识点)
- php 小数整理,向上取整,向下去整,四舍五入,丢弃小数部分,保留整数部分
- php 小数整理,向上取整,向下去整,四舍五入,丢弃小数部分,保留整数部分
- 整理的一套php面试题(附有部分题的答案)
- 网络部分面试知识点整理
- 《分布式操作系统》部分知识点整理
- C++部分知识点整理
- 我整理的PHP 7.0主要新特性
- php 小数整理,向上取整,向下去整,四舍五入,丢弃小数部分,保留整数部分
- OCMock单元测试部分知识点整理
- php 小数整理,向上取整,向下去整,四舍五入,丢弃小数部分,保留整数部分
- 【旧代码整理】/project/library/function.php 基本function部分
- 整理系列-20161118- PHP 邮件接收功能实现「部分资源mark」