The new features of PHP 7

by Enrico Zimuel - @ezimuel
Senior Software Engineer
Zend Technologies, a Rogue Wave Company (USA)


Codemotion, Milan, 25th Nov. 2016

PHP

  • PHP: Hypertext Preprocessor
  • The most popular server-side language: PHP is used by 82.3% of all the websites (source: w3techs.com)
  • Used by Facebook, Wikipedia, Yahoo, Etsy, Flickr, Digg, etc
  • 21 years of usage, since 1995
  • Full OOP support since PHP 5

PHP ecosystem

PHP 7

Released: 3 December 2015

Last major was PHP 5, 13 July 2004 (11 years!)

Last release is 7.0.13 (10 Nov 2016)

PHP 6 ?

Unicode support at the core language level

Not released, project is abandoned

PHP deadlines

Version Release Supported until
5.3 30 June 2009 14 August 2014
5.4 1 March 2012 3 September 2015
5.5 20 June 2013 21 July 2016
5.6 28 August 2014 31 December 2018
7.0 3 December 2015 3 December 2018
7.1 December 2016? 3 years

PHP7 in a nutshell

  • Great performance (thanks to PHPNG)
  • Return/Scalar Type Declarations
  • Cryptographic secure pseudo-random number generator (CSPRNG)
  • Anonymous Classes
  • Combined Comparison (Spaceship) Operator
  • Null coalescing operator
  • New Error hierarchy

Performance

PHPNG, PHP Next Generation

Project by Dmitry Stogov (Zend)

New data structure management in the PHP engine

Great performance improvement!

Benchmark


$a = [];
for ($i = 0; $i < 1000000; $i++) {
  $a[$i] = ["hello"];
}
echo memory_get_usage(true);
PHP 5.6 PHP 7
Memory Usage 428 MB 33 MB
Execution time 0.49 sec 0.06 sec

Other languages

PHP 7 is also faster than Python 3!

Wordpress

Case studies

  • Badoo saved one million dollars switching to PHP 7 (source)
  • Tumblr reduced the latency and CPU load by half moving to PHP 7 (source)
  • Dailymotion handles twice more traffic with same infrastructure switching to PHP 7 (source)

Return Type Declarations


function foo(): array {
    return [];
}

function bar(): DateTime {
    return null; // invalid
}

More information: PHP documentation

Scalar Type Declarations


declare(strict_types=1);

function sendHttpStatus(int $statusCode, string $message) {
    header('HTTP/1.0 ' .$statusCode. ' ' .$message);
}
sendHttpStatus(404, "File Not Found"); // ok
sendHttpStatus("403", "OK"); // fatal error

function add(float $a, float $b): float {
    return $a + $b;
}
add(1, 2); // float(3)

More information: PHP documentation

CSPRNG

Cryptographically secure pseudo-random number generators


// generates a random number between 1 and 100
$num = random_int(1,100);

// generates a random string of 1 Kb
$bytes = random_bytes(1024);

More information: PHP documentation

Anonymous Classes


/* return an anonymous class */
return new class($controller) implements Page {
    public function __construct($controller) {
        /* ... */
    }
    /* ... */
};

class Foo {}
$child = new class extends Foo {};
var_dump($child instanceof Foo); // true

More information: PHP documentation

Spaceship operator

A <=> B

0 if A == B, 1 if A > B, -1 if A < B


echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1

Null coalescing

?? operator


// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalescing can be chained
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';

New Error hierarchy

Throwable: Error, Exception


function call_method($obj) {
    $obj->method();
}

try {
    call_method(null); // oops!
} catch (Error $e) {
    printf ("Error: %s\n", $e->getMessage());
} catch (Exception $e) {
    printf ("Exception: %s\n", $e->getMessage());
}

More information: PHP documentation

Much more...

  • Consistent 64-bit support
  • Fix list() behavior inconsistency
  • Filtered unserialize()
  • Group use declarations
  • Closure::call()
  • Expectations
  • ...

Check the PHP 7 new features at php.net

PHP 7.1

  • Nullable types
  • Void return types
  • Deprecation of Mcrypt ext
  • Support of Authenticated Encryption in OpenSSL
  • Support class constant visibility
  • Iterable pseudo-type
  • ...

Check the PHP 7.1 new features at php.net

Thanks!

Contact me: enrico.zimuel [at] roguewave.com

Blog and web site: www.zimuel.it

Follow me: @ezimuel



Creative Commons License
This work is licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.
I used reveal.js to make this presentation.