The new PHP
PHP is experiencing a renaissance, with improvements and new standards.
The programming language many love to hate is experiencing a renaissance. This is not your parents’ PHP. The new PHP is a more mature language with community standards, interoperable components, and a passionate movement toward improved performance. If you bypassed PHP for alternative languages, or if you are a PHP veteran unaware of recent changes, you should absolutely give PHP a second look.
Language features
PHP 7.0 (the latest stable version as I’m writing this) has made tremendous progress from earlier versions. Recent PHP releases contain powerful new features and helpful developer tools including:
- Namespaces;
- Traits;
- Scalar type hints;
- Return type declarations;
- Secure psuedo-random number generator;
- Password hashing API;
- New error and exception handling system;
- Built-in web server;
- Built-in FastCGI process manager;
- Built-in phpdbg debugger;
Many of these features have accumulated since PHP 5.4. Perhaps the most notable feature of PHP 7.0, however, is raw performance. PHP 7.0 packages these powerful features into a codebase that is up to twice as fast as PHP 5.6. WordPress, Drupal, Symfony, Laravel, and other PHP frameworks all see tremendous speed increases thanks to PHP 7.0.
Interoperable components
A few years ago, PHP had only a handful of frameworks (e.g. CakePHP, CodeIgniter, and so on). Each framework was an island and provided its own implementation of features commonly duplicated in other frameworks. Unfortunately, these insular implementations were often incompatible with each other; PHP developers had to lock themselves to a specific framework for a given project.
Today the story is different. The new PHP community uses package management and component libraries to mix and match the best available tools. I like to compare the new PHP ecosystem to grocery shopping. If I need to consume a remote API, I’ll visit aisle 3 and pick up Guzzle. Do I need a request router? Symfony, Aura, Slim, and FastRoute are on aisle 4. You get the gist. The new PHP is about interoperable components using their comparative advantage to provide the best combination of ingredients for your project.
The easiest way to start using PHP components is to install the Composer package manager and browse the Packagist component repository.
Community standards
Because the new PHP community is large and diverse with many PHP components, it is important that components adhere to shared interfaces and common code style guidelines. Shared interfaces help PHP developers implement new features without reinventing wheels. Common code style guidelines reduce learning curves and allow different developers to read and contribute to shared code.
The PHP Framework Interop Group (PHP-FIG) is an unofficial but authoritative group of PHP framework developers and community representatives whose goal is “to talk about the commonalities between our projects and find ways we can work together. The PHP-FIG has passed several standards so far:
- PSR-1 (Basic code style);
- PSR-2 (Strict code style);
- PSR-3 (Logging interfaces);
- PSR-4 (Autoloading);
- PSR-7 (HTTP message interfaces);
These standards propose file, class, and namespace conventions, code style guidelines, and a set of shared interfaces to encourage component and framework interoperability.
The PHP-FIG is by no means the law of the land, but its suggested standards are adopted by many of the most popular PHP frameworks. Its goals are admirable, and it welcomes feedback. I highly encourage you to consider implementing the PHP-FIG standards in your PHP code and to submit feedback on future PHP-FIG proposals.
Performance
There are also exciting things happening with PHP under the hood, too. The Zend Engine included with PHP 7.0 provides up to twice the performance of PHP 5.6. This is possible thanks to reduced memory usage and optimized heap and stack utilization. In practical terms, most modern PHP frameworks will realize serious performance gains with PHP 7.0, essentially for free.
Facebook continues to make great progress on its alternative open-source PHP engine, the HipHop Virtual Machine (HHVM). HHVM uses just-in-time compilation to provide great performance while still allowing the dynamic interpretation to which PHP developers are accustomed. PHP 7.0, however, narrows the performance gap with HHVM. PHP 7.0 and HHVM are leveraging each other’s momentum to propel themselves further in terms of performance and features. It truly is an exciting time to be a PHP developer.
Resources
Here are a few resources to help you get up to speed with PHP 7.0:
- https://php.net/docs.php
- http://php.ug/
- http://www.php-fig.org/
- http://www.phptherightway.com/
- https://www.phptoday.org/php7
- https://nomadphp.com/
- https://phpmentoring.org/
- https://getcomposer.org/
- https://packagist.org/
Find out more about the new PHP. Browse PHP ebooks and videos at shop.oreilly.com