Home > Backend Development > PHP Tutorial > A brief discussion on the major new features of php7_php skills

A brief discussion on the major new features of php7_php skills

WBOY
Release: 2016-05-16 20:06:08
Original
1448 people have browsed it

So far, PHP has officially released the RC5 version of php7, and the first official version is expected to be released around November! Now, the major features of php7 have definitely been finalized and there will be no further changes. The iterations of some subsequent versions are mainly bug fixes, optimizations and the like. Let’s talk about the major changes in php7 that we have been looking forward to. . .

Preview of new features

ZEND引擎升级到Zend Engine 3,也就是所谓的PHP NG
增加抽象语法树,使编译更加科学
64位的INT支持
统一的变量语法
原声的TLS - 对扩展开发有意义
一致性foreach循环的改进
新增 <=>、**、&#63;&#63; 、u{xxxx}操作符
增加了返回类型的声明
增加了标量类型的声明
核心错误可以通过异常捕获了
增加了上下文敏感的词法分析
Copy after login

Some features removed

1. Removed some old extensions and migrated them to PECL (for example: mysql)
2. Remove support for SAPIs
3. Tags like 4.Hexadecimal string conversion has been abolished

//PHP5
"0x10" == "16"

//PHP7
"0x10" != "16"
Copy after login


5.HTTP_RAW_POST_DATA has been removed (you can use php://input instead)
6. Static functions no longer support calling a non-static function through an incompatible $this
$o = & new className{}, this writing method is no longer supported
7. The php.ini file has removed # as a comment and uses ; to comment

Some behavioral changes

No longer supports function definition parameters with the same name
The use of the constructor with the same name of the type is no longer recommended (it has not been removed currently and will be removed later)
Keywords such as String, int, float, etc. cannot be used as class names
func_get_args() gets the value of the current variable

function test ($num) {
  $num++;
  var_dump(func_get_args()[0]);
};

test(1)

//PHP5
int(1)

//PHP7
int(2)

Copy after login

The following is a selection of some main, core, and important features for us PHPer to introduce

PHP NG

The new php engine has optimized many places, and it is precisely because of this that the performance of php7 has been improved nearly twice compared to php5!

Reconstruction of ZVAL structure

On the left is the zval of PHP5 (24 bytes), and on the right is the zval of PHP7 (16 bytes);

It can be seen that the zval of php7 is more complicated than that of php5, but it can be reduced from 24 bytes to 16 bytes. Why?

In C language, each member variable of struct occupies an independent memory space, while the member variables in union share a memory space (union is widely used to replace struct in php7). Therefore, although it seems that there are a lot more member variables, the actual memory space occupied by many of them is public and has decreased.

Replace the previous HashTale structure with the new Zend Array

The most used, most useful, most convenient and most flexible thing in our PHP program is the array, and the bottom layer of PHP5 is implemented by HashTable. PHP7 uses the new Zend Array type, which has better performance and access speed. Significant improvement!
Some very commonly used functions with low overhead are directly transformed into opcodes supported by the engine

call_user_function(_array) => ZEND_INIT_USER_CALL
is_int/string/array/* => ZEND_TYPE_CHECK
strlen => ZEND_STRLEN
defined => ZEND+DEFINED
Copy after login

Uses new memory allocation and management methods to reduce memory waste
Optimization of core sorting zend_sort

//PHP5 - 快速排序(非稳定排序)
array(1 => 0, 0 => 0)

//PHP7 - 快速排序+选择排序(稳定排序)
array(0 => 0, 1 => 0)

Copy after login

For elements less than 16, use selection sorting. For elements larger than 16, divide them into units of 16, use selection sorting respectively, and then combine them all and use quick sorting. Compared with the previous sorting, the internal elements have been changed from unstable sorting to stable sorting, which reduces the number of exchanges of elements and the number of operations on memory, and improves performance by 40%
Abstract syntax tree

If we have such a need now, we need to perform syntax detection on the PHP source file and implement coding standards. Before php5, there was no AST, and opcodes were generated directly from the parser! We need to use some external php syntax parsers to achieve this; and php7 adds AST, we can implement such an extension ourselves, and use the functions provided by the extension to directly obtain the AST structure corresponding to the file, and this structure is what we It can be identified, so we can do some optimization and judgment on this basis.

64-bit INT support

Supports storage of strings larger than 2GB
Supports uploading files larger than 2GB
Ensure that strings on all platforms [64-bit] are 64bit
Unified syntax variables

$$foo['bar']['baz']

//PHP5
($$foo)[‘bar']['baz']

//PHP7: 遵循从左到右的原则
${$foo[‘bar']['baz']}

Copy after login

Improvements of foreach loop

//PHP5
$a = array(1, 2, 3);foreach ($a as $v){var_dump(current($a));}
int(2)
int(2)
int(2)

$a = array(1, 2, 3);$b=&$a;foreach ($a as $v){var_dump(current($a));}
int(2)
int(3)
bool(false)

$a = array(1, 2, 3);$b=$a;foreach ($a as $v){var_dump(current($a));}
int(1)
int(1)
int(1)

//PHP7:不再操作数据的内部指针了
$a = array(1, 2, 3);foreach ($a as $v){var_dump(current($a))}
int(1)
int(1)
int(1)

$a = array(1, 2, 3);$b=&$a;foreach ($a as $v){var_dump(current($a))
int(1)
int(1)
int(1)

$a = array(1, 2, 3);$b=$a;foreach ($a as $v){var_dump(current($a))}
int(1)
int(1)
int(1)

Copy after login

Several new operators

//<=> - 比较两个数的大小【-1:前者小于后者,0:前者等于后者,1:前者大于后者】
echo 1 <=> 2;//-1
echo 1 <=> 1;//0
echo 1 <=> 0;//1

// ** - 【a的b次方】
echo 2 ** 3;//8

//&#63;&#63; - 三元运算符的改进
//php5
$_GET['name'] &#63; $_GET['name'] : '';//Notice: Undefined index: …
//php7
$_GET['name'] &#63;&#63; '' -> '';

//\u{xxxx} - Unicode字符的解析
echo "\u{4f60}";//你
echo "\u{65b0}";//新

Copy after login

Declaration of return type

function getInt() : int {
  return “test”;
};

getInt();

//PHP5
#PHP Parse error: parse error, expecting '{'...

//PHP7
#Fatal error:Uncaught TypeError: Return value of getInt() must be of the type integer, string returned 

Copy after login

Declaration of scalar types

function getInt(int $num) : int {
  return $num;
};

getInt(“test”);

//PHP5
#PHP Catchable fatal error: Argument 1 passed to getInt() must be an instance of int, string given…

//PHP7
#Fatal error: Uncaught TypeError: Argument 1 passed to getInt() must be of the type integer, string given…

Copy after login

Core errors can be caught through exceptions

try {
  non_exists_func();
} catch(EngineException $e) {
  echo “Exception: {$e->getMessage();}\n”;
}

//这里用php7试了一下还是没法捕获,但是看鸟哥介绍说是可行的。。。
#Exception: Call to undefined function non_exists_func()

Copy after login

Sensitive lexical analysis

//PHP5
class Collection {public function foreach($arr) {}}
#Parse error: parse error, expecting `"identifier (T_STRING)”'...

//PHP7
class Collection {
  public function foreach($arr) {}
  public function in($arr){}
  public function where($condition){}
  public function order($condition){}
}
$collection = new Collection();
$collection->where()->in()->foreach()->order();

Copy after login

That’s almost it. I’ve basically finished my preliminary understanding of php7. There must be many wrong and low-level mistakes in it. I hope you guys can correct them in time so that I can correct them and take notes! hey-hey!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template