PHP 7使用新的Zend Engine 3.0将应用程序性能提升近两倍,内存消耗比PHP 5.6高出50%。它容许服务更多的并发用户,而不须要任何额外的硬件。PHP 7是考虑到今天的工做负载而设计和重构的。php
在PHP 7中,引入了一个新的特性,即标量类型声明。标量类型声明有两个选项数组
功能参数的如下类型可使用上述模式强制执行安全
强制模式
<?php // Coercive mode function sum(int ...$ints) { return array_sum($ints); } print(sum(2, '3', 4.1)); //9 ?>
严格模式
<?php // Strict mode declare(strict_types=1); function sum(int ...$ints) { return array_sum($ints); } print(sum(2, '3', 4.1)); //Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given, ... ?>
有效的返回类型
<?php declare(strict_types = 1); function returnIntValue(int $value): int { return $value; } print(returnIntValue(5)); ?>
无效返回类型
<?php declare(strict_types = 1); function returnIntValue(int $value): int { return $value + 1.0; } print(returnIntValue(5));//Fatal error: Uncaught TypeError: Return value of returnIntValue() must be of the type integer, float returned. ?>
在PHP 7中,引入了一个新的特性,即空合并运算符(??)。它用来替代与isset()函数结合的三元操做。该空若是它存在,而不是空合并运算符返回第一个操做数; 不然返回第二个操做数。session
<?php // fetch the value of $_GET['user'] and returns 'not passed' // if username is not passed $username = $_GET['username'] ?? 'not passed'; print($username); print("<br/>"); // Equivalent code using ternary operator $username = isset($_GET['username']) ? $_GET['username'] : 'not passed'; print($username); print("<br/>"); // Chaining ?? operation $username = $_GET['username'] ?? $_POST['username'] ?? 'not passed'; print($username); // output //not passed ?>
它用来比较两个表达式。当第一个表达式分别小于,等于或大于第二个表达式时,它返回-1,0或1。字符串比较ASCII闭包
//integer comparison print( 1 <=> 1);print("<br/>"); print( 1 <=> 2);print("<br/>"); print( 2 <=> 1);print("<br/>"); // output 0 -1 1
使用define()函数定义数组常量。在PHP 5.6中,只能使用const关键字来定义它们。并发
<?php //define a array using define function define('animals', [ 'dog', 'cat', 'bird' ]); print(animals[1]); // output cat ?>
如今可使用新类来定义匿名类。匿名类能够用来代替完整的类定义。app
<?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) { print($msg); } }); $app->getLogger()->log("My first Log Message"); ?> //output My first Log Message
Closure :: call()方法被添加为一个简短的方式来临时绑定一个对象做用域到一个闭包并调用它。与PHP5的bindTo相比,它的性能要快得多。dom
在PHP 7以前
<?php class A { private $x = 1; } // Define a closure Pre PHP 7 code $getValue = function() { return $this->x; }; // Bind a clousure $value = $getValue->bindTo(new A, 'A'); print($value()); //output 1 ?>
PHP 7+
<?php class A { private $x = 1; } // PHP 7+ code, Define $value = function() { return $this->x; }; print($value->call(new A)); //output 1 ?>
PHP 7引入了过滤的unserialize()函数,以便在对不可信数据上的对象进行反序列化时提供更好的安全性。它能够防止可能的代码注入,并使开发人员可以对能够反序列化的类进行白名单。函数
<?php class MyClass1 { public $obj1prop; } class MyClass2 { public $obj2prop; } $obj1 = new MyClass1(); $obj1->obj1prop = 1; $obj2 = new MyClass2(); $obj2->obj2prop = 2; $serializedObj1 = serialize($obj1); $serializedObj2 = serialize($obj2); // default behaviour that accepts all classes // second argument can be ommited. // if allowed_classes is passed as false, unserialize converts all objects into __PHP_Incomplete_Class object $data = unserialize($serializedObj1 , ["allowed_classes" => true]); // converts all objects into __PHP_Incomplete_Class object except those of MyClass1 and MyClass2 $data2 = unserialize($serializedObj2 , ["allowed_classes" => ["MyClass1", "MyClass2"]]); print($data->obj1prop); print("<br/>"); print($data2->obj2prop); //output 1 2 ?>
在PHP7中,增长了一个新的IntlChar类,它试图揭示额外的ICU功能。这个类定义了一些静态方法和常量,能够用来处理Unicode字符。在使用这个课程以前,你须要安装Intl扩展。性能
<?php printf('%x', IntlChar::CODEPOINT_MAX); print (IntlChar::charName('@')); print(IntlChar::ispunct('!')); //output 10ffff COMMERCIAL AT true ?>
在PHP 7中,引入了两个新的函数来以跨平台的方式生成密码安全的整数和字符串。
<?php $bytes = random_bytes(5); print(bin2hex($bytes)); //output 54cc305593 print(random_int(100, 999)); print(""); print(random_int(-1000, 0)); //output 614 -882 ?>
从PHP7开始,可使用单个use语句从相同的命名空间导入类,函数和常量,而不是使用多个use语句。
<?php // Before PHP 7 use com\tutorialspoint\ClassA; use com\tutorialspoint\ClassB; use com\tutorialspoint\ClassC as C; use function com\tutorialspoint\fn_a; use function com\tutorialspoint\fn_b; use function com\tutorialspoint\fn_c; use const com\tutorialspoint\ConstA; use const com\tutorialspoint\ConstB; use const com\tutorialspoint\ConstC; // PHP 7+ code use com\tutorialspoint\{ClassA, ClassB, ClassC as C}; use function com\tutorialspoint\{fn_a, fn_b, fn_c}; use const com\tutorialspoint\{ConstA, ConstB, ConstC}; ?>
PHP 7引入了一个新的函数intdiv(),它对它的操做数进行整数除法,并将除法运算返回为int。
<?php $value = intdiv(10,3); var_dump($value); print(" "); print($value); //output int(3) 3 ?>
session_start()函数接受来自PHP7 + 的一系列选项来覆盖php.ini中设置的会话配置指令。这些选项支持session.lazy_write,默认状况下,它会致使PHP在会话数据发生更改时覆盖任何会话文件。
添加的另外一个选项是read_and_close,它表示应该读取会话数据,而后应该当即关闭会话。例如,将session.cache_limiter设置为private,并使用如下代码片断将标志设置为在读取完毕后当即关闭会话。
<?php session_start([ 'cache_limiter' => 'private', 'read_and_close' => true, ]); ?>
PHP 4样式构造函数是与它们定义的类具备相同名称的方法,如今已被弃用,而且未来将被删除。若是PHP 4的构造函数是类中定义的惟一构造函数,则PHP 7将发出E_DEPRECATED。实现__construct()方法的类不受影响。
<?php class A { function A() { print('Style Constructor'); } } ?>
对非静态方法的静态调用已被弃用,并可能在未来被删除
<?php class A { function b() { print('Non-static call'); } } A::b(); // Deprecated: Non-static method A::b() should not be called statically in...Non-static call ?>
password_hash()函数的salt选项已被弃用,因此开发人员不会生成本身的(一般是不安全的)盐。当开发人员不提供盐时,函数自己会生成密码安全的盐,所以再也不须要定制盐的生成。
该capture_session_meta SSL上下文选项已被弃用。SSL元数据如今经过stream_get_meta_data()函数使用。
从PHP 7开始,错误处理和报告已经改变。而不是经过PHP 5使用的传统错误报告机制来报告错误,如今大多数错误都是经过抛出错误异常来处理的。与异常相似,这些错误异常会一直冒泡,直到它们到达第一个匹配的catch块。若是没有匹配的块,则使用set_exception_handler()安装的默认异常处理程序将被调用。若是没有默认的异常处理程序,那么异常将被转换为致命错误,并将像传统的错误同样处理。
因为错误层次结构不是从Exception扩展的,因此使用catch(Exception $ e){...}块来处理PHP 5中未捕获的异常的代码将不会处理这样的错误。catch(Error $ e){...}块或set_exception_handler()处理程序是处理致命错误所必需的。
<?php class MathOperations { protected $n = 10; // Try to get the Division by Zero error object and display as Exception public function doOperation(): string { try { $value = $this->n % 0; return $value; } catch (DivisionByZeroError $e) { return $e->getMessage(); } } } $mathOperationsObj = new MathOperations(); print($mathOperationsObj->doOperation()); // output Modulo by zero ?>
2017已经接近尾声,崭新的2018即未来临,在这个知识突飞猛进的时代,温故而知新。script maker!