PHP 7 – A look at the future

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.

I). 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".

II). 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.

III). 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.

a). 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 at point b.

b). 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.

c). 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.

d). 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.

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.

f). 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.

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.

g). 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.

IV). 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.

V). 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.

VI). 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.