New features in PHP 5.5

  sonic0002        2013-07-05 03:13:22       11,569        0    

Just a few days ago, the PHP official website was redesigned. It seems we directly go from Web 1.0 era to Web 2.0 era. In addition to this new change, PHP 5.5 was also released. Some new features are added in this release. Here we summarize some of them.

Enable OPCache by default

When installing PHP 5.5, the Zend OPCache will be compiled as OPCache by default and OPCache is enabled by default.

Some changes to the language itself

Add 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
}

Add finally keyword

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

Can use ClassName::class to get fully qualified class name

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();

Can call function directly in empty()

function test_false() {
    return false;
}

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

Can use list() in foreach()

$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];

Changes to standard library

Add password hashing API

The newly added password hashing functions are : 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';
}

Add some new functions

PHP Core adds some new functions such as array_column(),boolval(),json_last_error_msg(),cli_get_process_title() etc. There are also some newly added extension functions.

  • MySQLi

mysqli_begin_transaction()
mysqli_release_savepoint()
mysqli_savepoint()

  • Intl

IntlDateFormatter::formatObject()
IntlDateFormatter::getCalendarObject()
IntlDateFormatter::getTimeZone()
IntlDateFormatter::setTimeZone()

  • cURL

curl_file_create() and CURLFile class can be used to create CURLFile object and realize file upload.

// curl_file_create.php

/**
 * http://zrwm.com/uploadtest.php:
 * 
 */

$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);

The end result is:

# 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)
  }
}

New classes and interfaces

Please check here for details of the new classes and interfaces

One simple example about DateTime and 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

Deprecations and removals

  • Don't support Windows XP and Windows Server 2003 anymore
  • Extension mysql is deprecated, need to use MySQLi or PDO_MySQL
  • preg_replace /e is deprecated
  • Logo GUIDs is removed
  • Some functions of extension intl are deprecated (IntlDateFormatter::setTimeZoneID() and datefmt_set_timezone_id() are deprecated, can use IntlDateFormatter::setTimeZone() and datefmt_set_timezone(). instead)
  • Some functions of extension mcrypt are deprecated (mcrypt_cbc(),mcrypt_cfb(),mcrypt_ecb(),mcrypt_ofb() are deprecated)

If you find any new feature in PHP5.5, you can add in the comment below.

Source : http://www.zrwm.com/?p=7536

PHP 5.5  NEW FEATURES  GENERATOR 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

Adapter design pattern explained