For all those who heard that PHP 7 is going to be released this November (originally planned for the 12th but now delayed) you may be wondering what to expect and how to prepare yourself. There have been some really great improvements in language at its core as well as a lot of cool new features. There are all sorts of rumors and benchmarks out there stating that PHP 7 will be about twice as fast as PHP 5 even! Let's take a deeper look into what will be changing.

PHP 7 Performances Changes

PHP 7 will be twice as fast? No way, that can't be. How can the langauge have such a radical difference in speed? Well there are a number of major changes to the core of PHP which significantly reduce the CPU and memory usage. The executor is much faster and uses significantly less memory. To illustrate, a program which previously required 300 million CPU instructions in PHP5 will only require about 200 million in PHP7; about a 30% reduction in CPU usage. In addition, the compiler generates more efficient bytecode (roughly 30% fewer lines). These two things combined lead to much more efficient (and fast) code.

In addition to those major performance changes, the language parser itself has been completely rewritten, now using proper computer science principles. The parser is now based on the Abstract Syntax Tree model. In an abstract syntax tree, each node of the tree denotes a construct which occurs in the source code. The syntax is abstract in that it does not represent every detail appearing in the real syntax. For example, grouping parentheses will be implicit, and syntactic constructs such as if-condition-then expressions may be representated as a single node with three branches.

The Lexer is now context-sensitive with support for semi-reserved words. This means that reserved keywords can now be used as names for attributes, constants, and methods within classes, interfaces, and traits. For example, you can now have a class method called "for" or "if".

Problems Migrating to PHP7

The most important question to be asking now before getting started migrating all your applications is "what problems should I look out for while migrating to PHP 7?" Fortunately there aren't really too many issues you should encounter unless you have code which was written back when PHP4 was cool.

There are now some new reserved keywords, so you'll need to check and ensure that you don't have any classes with these names:

  • bool
  • int
  • float
  • string
  • null
  • false
  • true
  • resource
  • object
  • mixed
  • numeric

Certain variable syntaxes will now have a different meaning in PHP7 compared to PHP5. This change has been referred to as Uniform Variable Syntax. You should of course avoid writing code in this ugly and non-self-documenting way anyway, but unfortunately we all have legacy code laying around. This is something to look out for because it will not throw a compilation exception since this syntax is still valid, just with a different meaning now. Here are some examples:

$$foo['bar']['blah']
//In PHP5 means: 
${$foo['bar']['blah']}
//In PHP7 means: 
($$foo)['bar']['blah']
/*************/
$foo->$bar['blah']
//In PHP5 means: 
$foo->{$bar['blah']}
//In PHP7 means:
($foo->$bar)['blah']
/*************/
$foo->$bar['blah']()
//In PHP5 means: 
$foo->{$bar['blah']}()
//In PHP7 means:
($foo->$bar)['blah']()
/*************/
Foo::$bar['blah']()
//In PHP5 means: 
Foo::{$bar['blah']}()
//In PHP7 means:
(Foo::$bar)['blah']()

A lot of features which were previously deprecated have now finally been removed including:

  • The ereg extension
  • The old mysql extension (you should already be using mysqli or PDO by now hopefully)
  • Scoped calls of non-static methods from incompatible $this context
  • The & operator (for example: $x =& new X;)
  • The 'eval' modifier for the preg_replace function
  • Old PHP4 style constructors are now deprecated (i.e. methods with the same name as the class they are defined in)

New Features in PHP7

PHP 7 has a lot of really cool and highly anticipated features coming. Here are some of the most interesting.

Fatal Errors
All actions which resulted in "fatal errors" in PHP5 will now throw catchable exceptions in PHP7. This means you can always fail gracefully in all cases.

Built-In Code Coverage
The phpdbg debugger which was built into PHP 5.6 (which unfortunately almost no one knows about) has now been extended to support code coverage which is great for unit testing.

Return Type Declarations
You can now explicitly indicate the return type of a function:

class Foo {
    public function bar() : int {
        //do whatever
    }
}

In the above code, if the function bar() returns a non-integer value a TypeError will be thrown.

Scalar Type Hinting
Probably the most anticipated feature of PHP 7 is scalar type hinting. Unfortunately this is also probably the most disappointing new feature. As one would expect, the syntax will look like this:

class Foo {
    public function bar(int $a, string $b, float $c) {
        var_dump($a, $b, $c);
    }
}

That looks great, just like we all wanted. But unfortunately the default behavior isn't at all like we wanted it.

$foo = new Foo();
$foo->bar(3.14, 3, 3);
//Outputs: int(3), string(1) '3', float(3)

So what did it do? It didn't enforce the types strictly and throw an exception or even a warning like we would expect, but rather it simply casted the inputs to the specified types. This is called Coercive Scalar Type Declaration. That's lame, but wait, there's a way to force it to check the types strictly. Let's check it out:

declare(strict_types=1);
$foo = new Foo();
$foo->bar(3.14, 3, 3);
//Throws a TypeError

Great, problem solved. Nope! This unfortunately works only in a file-by-file basis. That means that every single file must explicitly declare strict typing. Every file. You can't just throw it in your front controller or your bootstrap file. Every file.

Anonymous Classes
The new release of PHP will also now support anonymous classes. Here's how it looks:

class Foo {
    public function bar() {
        $x = new class($this) {
            private $blah;
          
            public function __construct(Foo $foo){
               $this->foo = $foo;
            }
        };
    }
}

That's kinda cool maybe for writing unit tests, but for the most part it falls into the antipattern of Java-envy.

Null Coalesce Operator
The Null Coalesce Operator should be a pretty nifty new feature. It works like a typical coalesce operator like you might use in a MySQL query. Here's how it looks:

$foo = $bar ?? $blah ?? $blorp;

The result of $foo will be the first non-null value in the chain. This operator could definitely save some lines of code and make things cleaner; great for assigning default values.

The Spaceship Operator
The next cool new feature is now known as the "Spaceship" Operator (originally called the Combined Comparison Operator).

//This code in PHP 5:
$result = ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
//Can now be reduced to this in PHP 7:
$result = $a <=> $b;

Here's a list of some of other many features to expect in PHP 7:

  • Zend Engine 3 (performance improvements and 64-bit integer support on Windows)
  • AST-based compilation process
  • Added Closure::call()
  • Bitwise shift consistency across platforms
  • Generator delegation
  • Unicode codepoint escape syntax
  • Simpler and more consistently available CSPRNG API
  • Shorthand syntax for importing multiple items from a namespace

Sources:
Sebastian Bergmann, Creator of PHPUnit - WebTech Conference Munich 2015