The new PHP

PHP's experiencing a renaissance, with improvements and new standards

elephpant
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, a growing affinity for interoperable components, and a passionate movement to improve performance. If you have bypassed PHP for alternative languages, or if you are a PHP veteran unaware of recent changes, you owe it to yourself to give PHP a second look.

Language Features

PHP 5.5 (the latest stable build as of this writing) has made major progress from earlier versions. Recent PHP releases contain powerful new features and helpful developer tools, such as a built-in web server, generators for simpler iteration, and namespaces. With PHP 5.4, traits were introduced (a la Scala or Perl) to allow code reuse in single inheritance languages, as well as closures, which allow you to code PHP in a functional style. Other important features include the built-in FastCGI process manager and phpdbg debugger, and a new password hashing API that makes it easy to hash and securely manage passwords in PHP.

Interoperable Components

A few years ago, PHP had several large frameworks (e.g. CakePHP, CodeIgniter, and so on). Each framework was an island and provided its own implementation of features commonly found in other frameworks. Unfortunately, these insular implementations were likely not compatible with each other and forced developers to lock themselves in with 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 it 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\Routing, Aura\Router, Slim, Pux, and nikic/fast-route 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 start browsing the Packagist component repository.

Community Standards

Because the new PHP community is large and diverse with a myriad of components, it is important that components adhere to a set of code style guidelines and shared interfaces. This lets developers start using new components with a lower learning curve, and it lets components work together more easily via shared interfaces.

The PHP Framework Interop Group (PHP-FIG for short) is an unofficial but authoritative group of framework developers and PHP 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 four standards so far: PSR-0, PSR-1, PSR-2, and PSR-3. 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 being 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 PHP Zend Engine recently introduced memory usage optimizations. The memory usage in PHP 5.5 is far less than earlier versions. The PHP Zend Engine also provides a built-in FastCGI process manager that typically sits behind a reverse proxy (e.g. nginx) and will take of process spawning and management for you. This alleviates the need to embed new PHP instances in each Apache process using the Apache mod_php module.

Facebook has also made great progress on its alternative open-source PHP engine, the HipHop Virtual Machine (HHVM). HHVM uses a just-in-time compilation technique to provide incredible performance while still allowing the ease-of-use to which PHP developers are accustomed. Like the PHP Zend Engine, HHVM also includes FastCGI support. HHVM’s goal in early 2014 is to have 100% unit test pass for the top twenty PHP frameworks, and it recently announced a development roadmap for the next six months. Keep an eye on HHVM. I have a feeling it will drastically change the PHP landscape over the next few years.

Resources

Here are a few more resources to help you learn more about PHP.

Image via drewm

Related

Sign up for the O'Reilly Programming Newsletter to get weekly insight from industry insiders.
  • Claudiu Ion

    well done, Is good to se improve to any language

  • Manuel

    I wish those cool new APIs weren’t provided by packages but built in the core language. The way I see it, the main goal of those projects is to put the user as far as possible from the rudeness of the core, not an easy task.

    • http://www.zefwebdesign.co.uk Jozef Maxted

      Noooo, packages are the way to go, simply adding it all to the core introduces huge bloat. That is very much the old PHP mentality!

    • http://evertpot.com/ Evert

      PHP is in my mind the exception, where there’s already a *lot* of stuff part of core, as opposed to userland code. Look at python, perl, ruby, etc.. they’re all relatively slim languages, and packages provide the additional functionality.

      Adding stuff to the core will make the engine bloated.

  • Ziad Hilal

    Definitely feel that Laravel needs a mention, it’s a great framework that supports these type of conventions & improvements (php-fig, composer).

    • nXqd

      I think lavarel alone is helping PHP community a lot :)

      • Zqxd

        I think Yii framework did a great deal too.

    • Maxime Fabre

      Yup, Laravel is really missing from this article.

  • Gerob Kimball

    I’d like to point out that there are 5 standards with PHP-FIG, PSR-4 was accepted a few months back which improves on the autoloading standards of PSR-0.

  • Steven Leggett

    Arg, Disqus lost my comment. I think we’re finally starting to see the boat turn around where PHP developers are seeing the light of common reuse libraries across projects (even if they’re competing). AKA: Laravel uses about 15 Symfony components. We need more components that can be hot-swapped into projects that aren’t “framework” specific. Just consider all the code that is lost in proprietary framework’s bundles/modules. Imagine if that could be globally available to any other package.

    Other languages figured this out long ago: Rails has Gems. Python has pip. PHP “had” Pear but it never really caught on IMO. Now with composer gaining traction and becoming the standard package tool we have a place to share it.

    Example: Drupal has 25,731 Modules….. none of them can be shared with anyone else. That’s a real shame. I hope the leaders of these new frameworks, Laravel/Symfony and other ones can make sure that functional extensions (bundles) can also work across ecosystems. We don’t need more islands of functionality tied to a specific thing. What we need are more components to get s*** done.

  • nnnn

    And the destruction began with HHVM additions.. better switch to Java or ASP.NET

  • markseddon

    PHP has come along leaps and bounds since the heady days of PHP4. Those developers focused on stability have been held back with Ubuntu 12.04 LTS packaged with the now EOL PHP 5.3 but with Trusty coming out in a month or so I’m particularly excited about the prospect of using all of the PHP 5.5 goodness (Opcache / Generators / all of the 5.4 features and so on) in production.

    It’s an exciting time for PHP, we’ve been the poor cousins to Ruby (and the ubiquitous Rails), but there’s a fire in the community that I’ve not seen for a while. We’ve got 3 strongly backed and community-driven frameworks in Symfony, Laravel, and ZF, and the FIG means that it’s increasingly easier to use components from any framework (or component library) throughout one’s codebase. Much better than that oft maligned PEAR.