Skip to content Skip to main navigation Skip to footer

Linux:PHP 5.5新特性

默认开启OPCache

PHP 5.5安装时候会默认编译Zend OPcache做为OPCache,并默认开启OPCache.

PHP语言的一些改变

增加Generator

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关键字

try {
    echo '1';
    throw new Exception();
} catch (Exception $e) {
    echo '2';
} finally {
    echo '3';
}

完全限定类名

使用ClassName::class可以获得”完全限定类名”(fully qualified class name)

namespace FooBar;
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( )函数允许直接调用函数

function test_false() {
    return false;
}
if (empty(test_false()))
{
    echo "output something.";
}

foreach中使用list( )

$users = [
    ['Foo', 'Bar'],
    ['Baz', 'Qux'],
];
foreach ($users as list($firstname, $lastname)) {
    echo "First name: $firstname, last name: $lastname.";
}

常量寻址(Constant dereferencing)-访问常量内元素

echo "hello"[1];
echo [1,2,3,4][3];

标准库或函数/方法的一些改变

增加密码哈希API

password hashing api的函数:password_get_info(), password_hash(),password_needs_rehash(),password_verify()

$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()等函数。

此外还新增一些有用的扩展函数/方法:

  • MySQLimysqli_begin_transaction()mysqli_release_savepoint()mysqli_savepoint()
  • IntlIntlDateFormatter::formatObject()IntlDateFormatter::getCalendarObject()IntlDateFormatter::getTimeZone()IntlDateFormatter::setTimeZone()
  • cURLcURL新增一些函数,其中curl_file_create()函数或CURLFile类都可以创建CURLFile对象,实现文件的上传功能.

新增的类/接口

查看PHP 5.5新增的类/接口.

对比DateTime与DateTimeImmutable的一个简单例子:

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

过期与删除的特性

  • 不再支持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()等函数过期
via http://www.zrwm.com/?p=7536 ,有修改。
0 Comments

There are no comments yet

Leave a comment

Your email address will not be published.