There are a lot of cool things coming in PHP 7, so I thought I would do a quick rundown of some of the things I am most looking forward to.

Performance

PHP 7 promises to be the fastest PHP ever, this is a great bonus to all the nice new features we are getting. Performance will now be on par with HHVM, big tip hat to HHVM team for pushing PHP in the right direction.

Anonymous Classes1

This is one of my favourite features in Java, and really helps when you want to define a contract for client code to implement without having to create concrete classes of an interface.

Basically you can now do:

<?php  
var_dump(new class() {  
    public function __construct() {
    }
});

Looks pretty mundane on its own, so lets look at a more complete example.

If we wanted to write a Curl client and we want to force clients to handle the response/error in a known way using a contract (interface) we can do this using anon classes. Below is an example of such a client.

<?php

class Curly  
{
    // Make a request and handle the result
    public static function request($url, Handler $handler)
    {
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => $url,
            CURLOPT_FAILONERROR => true,
        ]);
        $response = curl_exec($curl);

        if (!curl_exec($curl)) {
            $handler->onError(
                [curl_error($curl), curl_errno($curl)]
            );
            curl_close($curl);
            return;
        }

        $handler->onResponse($response);
        curl_close($curl);
    }
}

// Interface client code must implement to send a request
interface Handler {  
    public function onResponse($response);

    public function onError($error);
}

// Client code implementation (success)
Curly::request('https://example.org',  
    new class() implements Handler {
        public function onResponse($response)
        {
            echo "Got response";
            var_dump(htmlentities($response));
        }

        public function onError($error)
        {
            echo "Got error";
            var_dump($error);
        }
});

// Client code implementation (error)
Curly::request('https://exampleerrorplease.org',  
    new class() implements Handler {
        public function onResponse($response) 
        {
            echo "Got response";
            var_dump(htmlentities($response));
        }

        public function onError($error)
        {
            echo "Got error";
            var_dump($error);
        }
});

In versions prior to 7 we would need to create a class (and a file) for each handler, now we can simply use anon classes to implement the handlers.

This approach works great for things like handlers and event listeners, where you want to give the client a clean way to attach to your object and where the client code varies and a single handler class would not work.

Exceptions In The Engine2

This is a big one if you have ever tried to setup decent monitoring in a PHP app. The problem with older PHP versions was that things like Fatal errors would kill the script and there was no way to capture those errors even using a custom error handler. PHP 7 fixes this by making all those pesky fatals into exceptions!

try {  
    $that->method_doesnt_exist();
} catch (EngineException $e) {
    $logger->alert('OMG a fatal', $e);
}

So now we can actually catch fatals and log them to our favourite ops dashboard or start paging people to wake up without having to scrape php logs! This is a big fat win.

Return Types3

One of the bigger changes is we can now define return types, using the syntax first used by Hack.

function blah() : string {  
    return "foo";
}

This is a great addition and will help stop bugs and we no longer have to rely on @return annotations to document return types. Return types operate in two modes weak and strict. In weak mode (the default) PHP will juggle the returned type and cast it to the defined type, in strict mode a TypeError exception is thrown. Obviously if you define custom type returns they always act in a strict manner because PHP cant juggle user defined types.

strict

declare(strict_types=1); // Enable strict typing

function blah() : string {  
    return 1;
}
blah();

Result:  
Fatal error: Uncaught TypeError: Return value of blah() must be of the type string, integer returned  

weak

function blah() : string {  
    return 1;
}
var_dump(blah());

Result: string(1) "1"  

Scalar Type Hinting4

In addition to return types one of the most hotly debated features was scalar type hinting. There were various arguments about the implementation and how strict it would be, we eventually ended up with a solution similar to Javascripts use strict which gives by default weak typing and optionally strict type checking.

strict

declare(strict_types=1); // Enable strict typing

function blah(string $bar) : string {  
    return "blah";
}
blah(1);

Result:  
Fatal error: Uncaught TypeError: Argument 1 passed to blah() must be of the type string, integer given  

weak

function blah(string $bar) {  
    return $bar;
}
var_dump(blah(1));

Result: string(1) "1"  

With both scalar type hinting and return types, it will be interesting to see whether strict or weak typing wins in terms of adoption. I know for one we have some hard choices to make at DeviantArt when we switch to PHP 7 in terms of coding standards.

Conclusion

I am really looking forward to working with PHP 7, I think the team have done some great work on the new features and generally they seem to be the right way for PHP to go.