This article was originally published in number 40 of Today Software Magazine in October 2015. You can see its Romanian version here and its English version here. You can also see me here presenting the article at Today Software Magazine’s launching event.

The PHP language started as a simple collection of C binaries used for maintaining a simple web page. Today, PHP runs on millions of webservers and is ranked very high on the list of the most popular programming languages in the world . Its most intense growth of popularity happened in the first half of the 2000s, followed by an accelerated development and maturing process of tools and frameworks of this ecosystem.

Now, in 2015, with the release of the language’s next generation (PHP 7), it’s a good time to try and predict what the future holds, with a particular focus on PHP 7’s new features.

Bad Reputation

PHP’s high popularity, combined with an easy learning curve, attracted many beginner programmers who popularized many sub-optimal and unsafe programming practices. Given the multitude of defects and shortcomings of PHP implementation, led to multiple criticisms from the programming community and PHP ended up with a label of “second-hand language” which was “not used by real programmers”.

Current Status

The current situation of PHP’s ecosystem is very different from the one described above.

As I mentioned in the article’s introduction: in the last few years, many frameworks and tools have evolved a lot and started a migration to high-quality, clean and mature software tools.

The best example of such a tool is the appearance of the Composer project in 2011, a tool inspired by “npm”, which provides dependency management functionality for a project. Composer had a great contribution to PHP’s maturation because it allowed the use of correct, modern, easy and robust software practices. Pretty much all modern PHP frameworks use Composer for dependency management (Symfony 2, Zend Framework, CakePHP, CodeIgniter etc.).

Another example for the development of the language is the focus on correct implementation and usage of object-oriented concepts. Most frameworks and libraries migrated to such code; their users were encouraged to do the same.

Major new features of PHP 7

PHP’s new major version 7 is planned to be released in November 2015. We’ll list most main new features and, based on them, we’ll try to predict how the PHP language and its ecosystem will evolve in the following years.

AST-based Compilation

Given how interpreted languages (like PHP) are implemented, it’s expected that low performance would be common among its criticisms. Switching to an AST-based compilation was inspired by Facebook’s HipHop VM project. This will allow simplified maintenance of PHP’s implementation (more modular code), significantly increased performance and will allow implementing the feature mentioned in the next point.

531px-Abstract_syntax_tree_for_Euclidean_algorithm.svg

Uniform Variable Syntax

This feature introduces consistent left-to-right variable dereferencing; this way, more complex expressions will be correctly interpreted. Some examples include property content used as object name or using the result of an anonymous function as class name.

Examples:

$foo['bar']->baz->oof()::$rab
explode('|', $x)[3]()
$foo()['bar']()
foo()()
(function() { return 'func'; })()
'Foo'::$bar

Type Declarations

A feature inspired by Facebook’s Hack language is declaring the data types received (parameters) and returned by functions or methods. Until now, only declaration of parameter type was supported and only class names or “array” could be used in this case.

If the programmer wanted to document such information, he could only do it by using PHPDoc. But now, these declarations can be added for any data type, including primitive types; this applies to both function parameters and return type.


class MyClass
{
    function a() //return type is optional
    {
        return 123;
    }

    function b(): int
    {
        return 123;
    }

    function c(): ClassB
    {
        //fatal error - can't return null here
        return null;
    }
}

Anonymous Classes

Before PHP 7, PHP supported only anonymous functions; anonymous classes did not exist in the language. This is unlike other programming languages, which supported this feature for many years.

The syntax of declaring an anonymous class is very similar to other languages (like Java, C# etc.) and its use cases are similar, too.

Examples:


$x = new class(123) {
    public function __construct($a) {
        $this->a = $a;
    }
};

(new class extends A implements B {
    public function doStuff() { }
})->doStuff();

class MyClass extends A {
    public function getInstance() {
        return new class implements B { };
    }
}

Exceptions instead of Errors

In many exceptional situations (parse error, incorrect arithmetic operations, wrong type used etc.) PHP throws errors or warnings, which prevents executing destructors or “finally” blocks.

Because of this, many errors were replaced with language-provided exceptions. PHP’s syntax and keywords are much better at handling exceptions than error, it’s easier and more natural to write and maintain.

exceptions.hierarchy

Context Sensitive Lexer

This feature allows designing APIs that are as natural as possible, it was made possible by the AST-based compilation described at point a.

For example, you can now declare methods or functions that have identical names with various keywords of the PHP language e.g. list, and, or etc.


Finder::for('project')
    ->where('name')->like('%secret%')
    ->and('priority', '>', 9)
    ->or('code')->in(['4', '5', '7'])
    ->and()->not('created_at')->between([$t1, $t2])
    ->list($limit, $offset);

The lexical analysis stage will detect the context where that name is used (hence the name of the feature) and will determine if the use of that name in that particular context is valid.

Unicode Code Point Escape Syntax

This feature adds correct and consistent syntax for using Unicode characters. The syntax is like “u{xxxxx}” with a variable length, which means it allows more than 16 bits for the length.

The PHP Language Specification

We would be careless if we wouldn’t mention an important milestone of the PHP language: its formal specification. Even though PHP is 20 years old, until now, no one wrote such a specification for the language. Last year, though, a group of people decided it’s time for something like this and they did it. You can find it here: https://github.com/php/php-langspec.

The future of PHP

AST-based compilation and the new type declarations suggest that the PHP community is focusing on performance and are preparing for the possibility of JIT compilation; PHP is heavily influenced in this regard by Hip-Hop VM.

Replacing the errors with exceptions and adding anonymous classes assures us that object-oriented-related features will continue to be heavily developed and used in the next few years.

Also, a more powerful support for Unicode characters confirms that the needs for the future are internationalized websites in multiple languages. This is especially true in the case of Asian and African languages, because the high rise of Internet connections in those regions translates to a much broader audience.

Conclusion

If we take a look at PHP 7, the language and its ecosystem are going in the right direction. The heavy development and maturation of its libraries and frameworks is a strong proof for this. The new PHP language generation is not a revolution, but a simple evolution, one that will make programmers’ lives easier.

Leave a Reply

Your email address will not be published. Required fields are marked *