作者:南丞 时间:2017-12-15 02:28
新特性介绍:
ZVAL
大小从24字节减少到16字节
Zend Array(HashTable)
HashTable大小从72字节减少到56字节
HashTable bucket大小从72字节减少到32字节
函数调用的优化
使用了新的内存分配,管理方式,减少了内存的浪费
Immutable array optimization
<?php
$arr = [];
for($i=0; $i<100000; $i++) {
$arr[] = ['php'];
}
p(memory_get_usage(true));
//PHP5: 45M
//PHP7: 10M
call_user_function(_array) => ZEND_INIT_USER_CALL
is_int、is_string、is_array、... => ZEND_TYPE_CHECK
strlen => ZEND_STRLEN
defined => ZEND+DEFINED
PHP5(zend_qsort)
快速排序(非稳定排序)
array(1 => 0, 0 => 0)
PHP7(zend_sort)
快速排序+选择排序(稳定排序)
array(0 => 0, 1 => 0)
小于16个元素的使用选择排序,大于16个按照16个为单位去分割,分别使用选择排序,然后再全部合起来使用快速排序。排序较之前相比,内部元素由非稳定排序变成稳定排序,减少元素的交换次数,减少对内存的操作次数,性能提升40%
$$foo['bar']['baz']
PHP5: ${$foo[‘bar’]['baz']}
PHP7: ($$foo)[‘bar’][‘baz']【从左至右法则】
(function() {})();
$foo()();
[$obj, 'method']();
class A {
public static function a1() {}
}
[new A, 'a1']();
$f = function() {
p($this->name);
};
class F {
private $name = 'F';
}
$f->call(new F);
function getAnonymousClass($config) {
return new class(config) {};
}
p(getAnonymousClass(array()));
//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)
<?php
//PHP5
function compare($a, $b) {
return ($a < $b) ? -1 : (($a >$b) ? 1 : 0);
}
//PHP7
function compare($a, $b) {
return $a <=> $b;
}
2 ** 2; // 2 * 2 = 4
2 ** -1; // 1 / 2 = 0.5
3 ** -2; // 1 / 9 = 0.111111111
$a = null;
$b = 1;
$c = 2;
echo $a ?? $b , ‘,’ , $c ?? $b; // 1,2
echo $a ?? $b ?? $c , ‘,’ , $a ?? $d ?? $c; // 1,2
\u{xxxx}
echo "\u{4f60}";//你
echo "\u{65b0}";//新
// 从右至左强制
echo"\u{202E}iabgnay\u{1F602}";;
function getInt() : int {
return 'test';
};
getInt();
//返回值为DateTime的函数
function getDateTime() : DateTime {
return new DateTime();
};
function getAmount(int $num) : int {
return $num;
};
getAmount('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…
getAmount('123');
#PHP7新增的严格模式选项开启下也会报错【declare(strict_types=1),注意要放到代码的第一行】
try {
non_exists_func();
} catch(EngineException $e) {
echo "Exception: {$e->getMessage();}\n";
} finally {
echo "undefined function…";
}
//这里用php7试了一下没有捕获成功【但是确实抛出了异常】。。。
#Exception: Call to undefined function non_exists_func()
//PHP5
class Collection {public function foreach($arr) {}}
#Parse error: parse error, expecting `"identifier (T_STRING)”’...
//PHP7
class Collection {
public function foreach($arr) {
return $this;
}
public function in($arr){
return $this;
}
public function sort($condition){
return $this;
}
public function echo($condition){
return 'ok';
}
}
$collection = new Collection();
$collection->in()->foreach()->sort()->echo();
mysql、ereg
mysql 移到了 ext/pecl 中去了,ereg 移到了 ext/pcre
Unsafe curl file uploads (use CurlFile instead) //PHP5 curl_setopt(ch, CURLOPT_POSTFIELDS, array( ‘file’ => ‘@’.realpath(‘image.png’), ));
//PHP7 curl_setopt(ch, CURLOPT_POSTFIELDS, [ ‘file’ => new CURLFile(realpath(‘image.png’)), ]);
set_magic_quotes_runtime();
magic_quotes_runtime();
//(use stream_set_blocking() instead)
set_socket_blocking();
//(use mcrypt_generic_deinit() instead)
mcrypt_generic_end();
//(use mcrypt_encrypt() and mcrypt_decrypt() instead)
mcrypt_ecb();
mcrypt_cbc();
mcrypt_cfb();
mcrypt_ofb();
//(use datefmt_set_timezone() or IntlDateFormatter::setTimeZone() instead)
datefmt_set_timezone_id();
IntlDateFormatter::setTimeZoneID();
//(use XsltProcessor::setSecurityPrefs() instead)
xsl.security_prefs;//php.ini
//(use php.input_encoding、php.internal_encoding and php.output_encoding instead)
iconv.input_encoding;
iconv.output_encoding;
iconv.internal_encoding;
mbstring.http_input;
mbstring.http_output;
mbstring.internal_encoding;
(use PDO::ATTR_EMULATE_PREPARES instead)
PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT;//driver option
//(use peer_name instead)
CN_match;//SSL context options
SNI_server_name;//SSL context options