php7 新增功能实例总结

2022-04-15 0 761
目录
  • ①PHP标量类型与返回值类型声明
  • ②PHP NULL 合并运算符
  • ③PHP 太空船运算符(组合比较符)
  • ④PHP 常量数组
  • ⑤PHP Closure::call()
  • ⑥PHP CSPRNG
  • ⑦PHP 匿名类
  • ⑧PHP 7 use 语句

本文实例总结了php7 新增功能。分享给大家供大家参考,具体如下:

一、核心

  • 增加了group use语法声明。RFC: https://wiki.php.net/rfc/group_use_declarations
  • 增加了null合并运算符??。RFC: https://wiki.php.net/rfc/isset_ternary
  • 64位PHP7字符串长度可以超过2^31次方字节。
  • 增加了Closure::call()方法。
  • 双引号字符串和heredocs里面支持使用\u{xxxxx}来声明unicode字符。
  • define()可以把一个数组定义为常量。
  • 增加了合并比较运算符<=>。RFC: https://wiki.php.net/rfc/combined-comparison-operator
  • 增加了yield from 操作符。https://wiki.php.net/rfc/generator-delegation
  • 关键词在特定的场景中也可以使用了。RFC: https://wiki.php.net/rfc/context_sensitive_lexer
  • 增加了标量类型声明功能。RFC: https://wiki.php.net/rfc/scalar_type_hints_v5
  • 增加接口为用户层提供安全方便的随机数生成器。RFC: https://wiki.php.net/rfc/easy_userland_csprng

①PHP标量类型与返回值类型声明

标量类型声明

默认情况下,所有的PHP文件都处于弱类型校验模式。

PHP 7 增加了标量类型声明的特性,标量类型声明有两种模式:

  • 强制模式 (默认)严格模式

  • 严格模式

标量类型声明语法格式:

declare(strict_types=1);

代码中通过指定 strict_types的值(1或者0),1表示严格类型校验模式,作用于函数调用和返回语句;0表示弱类型校验模式。

可以使用的类型参数有:

  • int

  • float

  • bool

  • string

  • interfaces

  • array

  • callable

强制模式实例:

<?php 
// 强制模式 
function sum(int ...$ints) { 
 return array_sum($ints); 
} 
 
print(sum(2, '3', 4.1));
?>

以上程序执行输出结果为:

9

实例汇总将参数 4.1 转换为整数 4 后再相加。

严格模式实例:

<?php 
// 严格模式 
declare(strict_types=1); 
function sum(int ...$ints) { 
return array_sum($ints); 
} 
 
print(sum(2, '3', 4.1)); 
?>

以上程序由于采用了严格模式,所以如果参数中出现不适整数的类型会报错,执行输出结果为:

PHP Fatal error:  Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given, called in……

返回类型声明

PHP 7 增加了对返回类型声明的支持,返回类型声明指明了函数返回值的类型。

可以声明的返回类型有:

  • int

  • float

  • bool

  • string

  • interfaces

  • array

  • callable

返回类型声明实例:

实例中,要求返回结果为整数:

<?php 
declare(strict_types=1); 
function returnIntValue(int $value): int { 
return $value; 
} 
 
print(returnIntValue(5)); 
?>

以上程序执行输出结果为:

5

返回类型声明错误实例:

<?php 
declare(strict_types=1); 
function returnIntValue(int $value): int { 
return $value + 1.0; 
} 
 
print(returnIntValue(5)); 
?>

以上程序由于采用了严格模式,返回值必须是 int,但是计算结果是float,所以会报错,执行输出结果为:

Fatal error: Uncaught TypeError: Return value of returnIntValue() must be of the type integer, float returned…

②PHP NULL 合并运算符

PHP 7 新增加的 NULL 合并运算符(??)是用于执行isset()检测的三元运算的快捷方式。

NULL 合并运算符会判断变量是否存在且值不为NULL,如果是,它就会返回自身的值,否则返回它的第二个操作数。

以前我们这样写三元运算符:

$site = isset($_GET['site']) ? $_GET['site'] : 'PHP中文网';

现在我们可以直接这样写:

$site = $_GET['site'] ?? 'PHP中文网';

实例

<?php 
// 获取 $_GET['site'] 的值,如果不存在返回 'PHP中文网' 
$site = $_GET['site'] ?? 'PHP中文网'; 
print($site); 
print(PHP_EOL); // PHP_EOL 为换行符 
 
// 以上代码等价于 $site = isset($_GET['site']) ? $_GET['site'] : 'PHP中文网'; 
 
print($site); 
print(PHP_EOL); // ?? 链 $site = $_GET['site'] ?? $_POST['site'] ?? 'PHP中文网'; print($site); 
?>

以上程序执行输出结果为:

PHP中文网
PHP中文网
PHP中文网

③PHP 太空船运算符(组合比较符)

PHP 7 新增加的太空船运算符(组合比较符)用于比较两个表达式 $a 和 $b,如果 $a 小于、等于或大于 $b时,它分别返回-1、0或1。

实例

<?php 
// 整型比较 
print( 1 <=> 1);
print(PHP_EOL); 
print( 1 <=> 2);
print(PHP_EOL); 
 
print( 2 <=> 1);
print(PHP_EOL); 
print(PHP_EOL); 
// PHP_EOL 为换行符 
 
// 浮点型比较 
print( 1.5 <=> 1.5);
print(PHP_EOL); 
print( 1.5 <=> 2.5);
print(PHP_EOL); 
 
print( 2.5 <=> 1.5);
print(PHP_EOL); 
print(PHP_EOL); 
 
// 字符串比较 
print( "a" <=> "a");
print(PHP_EOL); 
print( "a" <=> "b");
print(PHP_EOL); 
print( "b" <=> "a");
print(PHP_EOL); 
?>

以上程序执行输出结果为:

0
-1
1
 
0
-1
1
 
0
-1
1

④PHP 常量数组

在 PHP 5.6 中仅能通过 const 定义常量数组,PHP 7 可以通过 define() 来定义。

实例

<?php
// 使用 define 函数来定义数组
define('sites', [ 'Google', 'PHP', 'Taobao']);
print(sites[1]);
?>

以上程序执行输出结果为:

PHP

⑤PHP Closure::call()

PHP 7 的 Closure::call() 有着更好的性能,将一个闭包函数动态绑定到一个新的对象实例并调用执行该函数。

实例

<?php 
class A {  
private $x = 1; 
} 
 
// PHP 7 之前版本定义闭包函数代码
 $getXCB = function() {  
return $this->x; 
}; 
 
// 闭包函数绑定到类 A 上 
$getX = $getXCB->bindTo(new A, 'A'); 
 
 echo $getX(); 
print(PHP_EOL); 
 
// PHP 7+ 代码 
$getX = function() {  
return $this->x; }; 
echo $getX->call(new A);
 ?>

以上程序执行输出结果为:

1
1

⑥PHP CSPRNG

CSPRNG(Cryptographically Secure Pseudo-Random Number Generator,伪随机数产生器)。

PHP 7 通过引入几个 CSPRNG 函数提供一种简单的机制来生成密码学上强壮的随机数。

  • random_bytes() – 加密生存被保护的伪随机字符串。

  • random_int() – 加密生存被保护的伪随机整数

random_bytes()

语法格式

string random_bytes ( int $length )

参数

  • length – 随机字符串返回的字节数。

返回值

  • 返回一个字符串,接受一个int型入参代表返回结果的字节数。

实例

<?php 
$bytes = random_bytes(5); 
print(bin2hex($bytes)); 
?>

以上程序执行输出结果为:

6f36d48a29

random_int()

语法格式

int random_int ( int $min , int $max )

参数

  • min – 返回的最小值,必须是大于或等于 PHP_INT_MIN 。

  • max – 返回的最大值,必须是小于或等于 PHP_INT_MAX 。

返回值

  • 返回一个指定范围内的int型数字。

实例

<?php 
print(random_int(100, 999)); 
print(PHP_EOL); 
print(random_int(-1000, 0)); 
?>

以上程序执行输出结果为:

723
-64

⑦PHP 匿名类

PHP 7 支持通过 new class 来实例化一个匿名类,这可以用来替代一些”用后即焚”的完整类定义。

实例

<?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; 
// 使用 new class 创建匿名类 
$app->setLogger(new class implements Logger { 
public function log(string $msg) {  
print($msg); 
} }); $app->getLogger()->log("我的第一条日志"); 
?>

以上程序执行输出结果为:

我的第一条日志

⑧PHP 7 use 语句

PHP 7 可以使用一个 use 从同一个 namespace 中导入类、函数和常量:

// PHP 7 之前版本需要使用多次 
use 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+ 之后版本可以使用一个 use 导入同一个 namespace 的类 
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}; 
?>

二、Opcache模块

  • 增加了基于文件的二级opcode 缓存机制。可以在php.ini文件中设置opcache.file_cache=<DIR>。当服务重启或者SHM重置的时候,使用二级文件缓存机制可以提高性能。
  • 也可以设置opcache.file_cache_only=1来限定只使用文件缓存。
  • 可以设置opcache.file_cache_consistency_checks=0参数来加快加载速度。
  • 可以设置opcache.huge_code_pages=0/1来决定是否将PHP code pages放到huage pages里面。http://www.laruence.com/2015/10/02/3069.html
  • windows版本增加了opcache.file_cache_fallback=1 配置项。

三、OpenSSL模块

增加了”alpn_protocols”选项。

四、反射

  • 增加了ReflectionGenerator类,用于yield from Traces, current file/line等等。
  • 增加了ReflectionType类,更好的支持新的返回值和标量声明功能。

五、流

windows版本增加了块读取的选项。可以通过传递array("pipe" => array("blocking" => true))参数来激活。

更多关于PHP相关内容感兴趣的读者可查看本站专题

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 PHP编程 php7 新增功能实例总结 https://www.niceym.com/15437.html