PHP 5.5新特性
-
集成Zend OPcache
PHP 5.5安装时候使用
--enable-opcache
编译Zend OPcache做为OPCache.
修改php.ini开启:[opcache] zend_extension=/usr/local/php550/so/opcache.so ; Determines if Zend OPCache is enabled opcache.enable=1
-
PHP语言的一些改变
- 增加Generator
<php function getLinesFromFile($fileName) { if (!$fileHandle = fopen($fileName, 'r')) { throw new RuntimeException('Couldn\'t open file "' . $fileName . '"'); } while (false !== $line = fgets($fileHandle)) { yield $line; } fclose($fileHandle); } foreach (getLinesFromFile($fileName) as $line) { // do something }
- 增加finally关键字
<php try { echo '1'; throw new Exception(); } catch (Exception $e) { echo '2'; } finally { echo '3'; }
- 使用ClassName::class可以获得”完全限定类名”(fully qualified class name)
<php namespace Foo\Bar; class One { const A = self::class; const B = Two::class; } class Two extends One { public static function run() { var_dump(self::class); var_dump(static::class); var_dump(parent::class); } } var_dump(One::class); $class = One::class; $x = new $class; var_dump($x); $two = Two::class; (new $two)->run();
- empty( )函数允许直接调用函数
<php function test_false() { return false; } if (empty(test_false())) { echo "output something."; }
- foreach中使用list( )
<php $users = [ ['Foo', 'Bar'], ['Baz', 'Qux'], ]; foreach ($users as list($firstname, $lastname)) { echo "First name: $firstname, last name: $lastname."; }
- 常量非关联(Constant dereferencing)
<php echo "hello"[1]; echo [1,2,3,4][3];
- 增加Generator
-
标准库或函数/方法的一些改变
- 增加密码哈希API
password hashing api的函数:password_get_info(), password_hash(), password_needs_rehash(),password_verify()<php $receved_password = "zrwmpassword"; $pass_hash = password_hash($receved_password, PASSWORD_DEFAULT); var_dump(password_get_info($pass_hash)); if (password_verify($receved_password, $pass_hash)) { echo 'Password is valid'; } else { echo 'Invalid password'; }
- 新增加的一些函数
PHP Core还新增了array_column(),boolval(),json_last_error_msg(),cli_get_process_title()等函数.
此外还新增一些有用的扩展函数/方法:- MySQLi
mysqli_begin_transaction()
mysqli_release_savepoint()
mysqli_savepoint() - Intl
IntlDateFormatter::formatObject()
IntlDateFormatter::getCalendarObject()
IntlDateFormatter::getTimeZone()
IntlDateFormatter::setTimeZone() - cURL
cURL新增一些函数,其中curl_file_create()函数或CURLFile类都可以创建CURLFile对象,实现文件的上传功能.<php // curl_file_create.php /** * http://zrwm.com/uploadtest.php: * <?php var_dump($_FILES); ?> */ $filename = '6908685_980x1200_0.jpg'; $url = 'http://zrwm.com/uploadtest.php'; // Get mimetype of the file $finfo = new finfo(FILEINFO_MIME_TYPE); $mimetype = $finfo->file($filename); $ch = curl_init($url); // Create a CURLFile object $cfile = curl_file_create($filename, $mimetype, 'myupload'); // Assign POST data $data = ['test_file' => $cfile]; curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_exec($ch);
运行结果:
# php curl_file_create.php array(1) { ["test_file"]=> array(5) { ["name"]=> string(8) "myupload" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(26) "/private/var/tmp/phprca3gH" ["error"]=> int(0) ["size"]=> int(84837) } }
- MySQLi
- 新增的类/接口
查看PHP 5.5新增的类/接口.
对比DateTime与DateTimeImmutable的一个简单例子:<php function printUTC1(DateTime $dt) { $dt->setTimeZone(new DateTimeZone('UTC')); echo $dt->format(DateTime::ATOM) . PHP_EOL; } function printUTC2(DateTimeImmutable $dt) { $dt->setTimeZone(new DateTimeZone('UTC')); echo $dt->format(DateTime::ATOM) . PHP_EOL; } $dt = new DateTime('now'); printUTC1($dt); // 2013-06-28T05:58:49+00:00 $dt = new DateTimeImmutable('now'); printUTC2($dt); // 2013-06-28T13:58:49+08:00
- 增加密码哈希API
-
过期与删除
- 不再支持Windows XP和 Windows Server 2003
- 扩展mysql过期,需使用MySQLi或PDO_MySQL
- preg_replace /e 修饰符过期
- 删除Logo GUIDs
- intl扩展的一些方法/函数过期
IntlDateFormatter::setTimeZoneID()和datefmt_set_timezone_id()现在已经过期.可以分别使用IntlDateFormatter::setTimeZone()和datefmt_set_timezone(). - mcrypt扩展的一些函数过期
mcrypt_cbc(),mcrypt_cfb(),mcrypt_ecb(),mcrypt_ofb()等函数过期