PHP 7 Quick Overview

      12 Comments on PHP 7 Quick Overview

Introduction

PHP7 is around the corner. According to the wiki time table if everything goes well, it is expected to be released in November 2015. Many of us are excited to learn the language level features and it’s performance.

Performance

Rasmus Lerdof in his slides for PHP Australia have covered a few benchmarking results of real world applications. One of the interesting slide is Lies, Damn Lies And Benchmarks.

I would like to look PHP7 with an eye on language level features than on a performance level, for, it is hard to do real benchmarks.

Let’s take a simple example,

According to http://3v4l.org/uhMNo/perf#tabs the time taken to finish on PHP7 and HHVM is greater than PHP 4.

Let’s look at another one

Running the above code shows two different results. http://3v4l.org/9P4pY/perf#tabs , http://3v4l.org/ZsROd/perf#tabs . The above examples are to demonstrate it depends on many factors and your application and Hello World benchmarks are misleading with the features each versions give you.

New Features

Null Coalesce Operator

If values are not set, php gives you undefined index notice. Normally what we do were check with isset as

Now with Null Coalesce Operator it makes much more simpler.

http://3v4l.org/84OnC

Scalar Type Hints

One of the interesting feature in PHP7 is scalar type hint. In earlier versions we were unable to type hint for int, string, float, bool. With PHP7 it is possible that you can do the same.

There are two modes of operation

  1. Weak
  2. Strict

By default the mode is weak. Switching between modes is possible with the help of declare statement

The declare statement must be the first line of the file and it applies only to that file. If we declare the type as strict this will affect all the internal functions of PHP and to any extensions installed.

Let us look into an example

Weak type mode

Passing a string value '1.2' will not throw any error/warning. It will automatically typecast to float. See http://3v4l.org/Hq55k

Now if we pass a 'string' value we get a Fatal error: Uncaught TypeError: Argument 1 passed to Item::setPrice() must be of the type float, string given. See http://3v4l.org/CmgqC#v700alpha2 .

So in a weak type mode if the values can be converted to appropriate types no errors or warnings are thrown, else we will get fatal errors.

Strict mode

Add declare(strict_types=1); as the first line in the above class Item example.

Now if we pass a string value '1.2', we will get a fatal error TypeError rather than trying to convert it to int as in the weak mode. See http://3v4l.org/FPaXh#v700alpha2 .

Handling Exceptions

Whether it is in weak or strict type mode, when the passed values are not the correct type expected we were getting fatal errors. With the RFC https://wiki.php.net/rfc/engine_exceptions_for_php7 and https://wiki.php.net/rfc/throwable-interface fatal errors can also be caught by Exceptions.

Every exceptions now implements Throwable interface. We can either type hint to Throwable or to appropriate exception.

TypeError exceptions are thrown when the type defined is wrong.

Return Type Declaration

With return types RFC it is now possible to define return types to function definition.

Defining return type helps to resolve unintended behavior.

Consider the below example.

If we are using a return type we get a TypeError http://3v4l.org/Bqqs7 , else we get a notice http://3v4l.org/Bqqs7 . So unexpected behaviors can be handled easily with return type declaration.

Reserve More Types in PHP 7

More reserved words are added with RFC.

So if your application have classes with name Int, Float, Bool, String, True, False or Null you will get fatal errors. So it is good to change them.

Timezone warning

When date.timezone settings in php.ini were not set, php was giving warning. With RFC if there is no timezone set the default is “UTC”. Don’t forget to change if you are dealing with time.

Summary

Lots of improved features are waiting for you, why not start playing with PHP7 and get your hands dirty to upgrade all your legacy systems.

Rasmus Lerdof have created vbox image https://github.com/rlerdorf/php7dev . Start playing with it. You can also use 3v4l.org to experiment with small scripts..