Outdated information is dangerous to unaware organizations and PHP developers who unknowingly create slow and insecure PHP applications or wrongly stay away from this powerful language that’s lately experiencing a renaissance. One should also avoid the impact of common rumors without understanding their origin or what’s the actuality.
So, let’s explore what the modern PHP is and how leading organizations around the world use it to create robust, secure, scalable, and maintainable web solutions.
How did we come to have the two worlds of PHP?
PHP is ubiquitous. Its use spans from non-developers who quickly learn to add few lines of PHP code to their hobby websites, all the way to some of the most known and powerful web apps like Facebook, Spotify, Slack, Etsy, and of course WordPress. This ubiquity, along with an easy entry point is what made PHP so hugely popular from the early days of the web until today. One can start with PHP by simply opening and closing PHP tags (<?php and ?>) within HTML code and inserting some simple code logic. The code would simply work without a complex server, programming language, or framework knowledge or setup.
This kind of great portability and flexibility is at the same time the biggest problem of PHP. No enforced structure, organization, and often lose syntax lead to messy, insecure, and difficult to maintain code. But PHP had too many strengths to be thrown away, thus today PHP is used by 79.1% of all the websites whose server-side programming language we know.
There were attempts to create alternatives which on a large scale proved unsustainable. Instead of throwing PHP into history, it proved more sustainable and reasonable to fix PHP. Now we have a new era of PHP with modernized PHP and cool, modern frameworks.
of all websites use PHP
The PHP renaissance
The best way to explain the recent PHP renaissance is to look at Slack which is one of the latest, trendiest “unicorns”. The “team communications” app is valued at around 4 billion dollars and it’s used by over 3 million users each day.
Slack uses PHP for most of its server-side application logic and
PHP gets several things very deeply, and uniquely, right.
- First, state. Every web request starts from a completely blank slate. Its namespace and globals are uninitialized, except for the standard globals, functions and classes that provide primitive functionality and life support. By starting each request from a known state, we get a kind of organic fault isolation; if request t encounters a software defect and fails, this bug does not directly interfere with the execution of subsequent request t+1. State does reside in places other than the program heap, of course, and it is possible to statefully mess up a database, or memcache, or the filesystem. But PHP shares that weakness with all conceivable environments that allow persistence. Isolating request heaps from one another reduces the cost of most program defects.
- Second, concurrency. An individual web request runs in a single PHP thread. This seems at first like a silly limitation. But since your program executes in the context of a web server, we have a natural source of concurrency available: web requests. Asynchronously curl’ing to localhost (or even another web server) provides a shared-nothing, copy-in/copy-out way of exploiting parallelism. In practice, this is safer and more resilient to error than the locks-and-shared-state approach that most other general-purpose languages provide.
- Finally, the fact that PHP programs operate at a request level means that programmer workflow is fast and efficient, and stays fast as the application changes. Many developer productivity languages claim this, but if they do not reset state for each request, and the main event loop shares program-level state with requests, they almost invariably have some startup time. For a typical Python application server, e.g., the debugging cycle will look something like “think; edit; restart the server; send some test requests.” Even if “restart the server” only takes a few seconds of wall-clock time, that takes a big cut of the 15–30 seconds our finite human brains have to hold the most delicate state in place.I claim that PHP’s simpler “think; edit; reload the page” cycle makes developers more productive. Over the course of a long and complex software project’s life cycle, these productivity gains compound.
Modern features of the core PHP language
So, as clarified above, “PHP gets several things very deeply, and uniquely, right”. It has a huge community, vast libraries of code, and yeah, it powers above 79% of the entire Internet. It only made sense to improve it and modernize it. And that’s exactly what happened.
Below are just some of the most notable “new PHP” features.
- Namespaces are an important concept that organizes PHP code into a hierarchy where. In addition to organizing a single application structure, namespaces allow each PHP component and/or framework to organize its code beneath its own globally unique vendor namespace so that it does not conflict with, or lay claim to, common class names used by other vendors.
Namespaces are important because they let us create sandboxed code that works alongside other developers’ code. This is the cornerstone concept of the modern PHP component ecosystem.
- Coding to an interface profoundly improves the ability to integrate third-party PHP components. In essence, an interface is a contract between two PHP objects that lets one object depend not on what another object is but, instead, on what another object can do. An interface decouples code from its dependencies, and it allows our code to depend on any third-party code that implements the expected interface.
- Traits enable modular implementations that can be injected into otherwise unrelated classes and encourage code reuse.
A trait is a partial class that can be mixed into existing PHP classes. Traits work double duty: they state what a class can do (like an interface), and they provide a modular implementation (like a class).
- Generators don’t require the implementation of Iterator interface in a heavyweight class. Instead, generators compute and yield iteration values on-demand which has profound implications for application performance.
Closures are functions that encapsulate their surrounding state at the time they were created. The encapsulated state exists inside the closure even when the closure lives after its original environment ceases to exist.
Zend OPcache speeds up PHP execution by opcode caching and optimization. It stores precompiled script bytecode in shared memory. As of version 7.0 it can store precompiled script bytecode on disk. This eliminates the stages of reading code from the disk and compiling it on future access. For further performance improvements, the stored bytecode is optimized for faster execution.
- Performance – with the release of new PHP versions came huge performance gains! PHP 7 and 8 allow the system to execute twice as many requests per second in comparison with the PHP 5.6, at almost half of the latency. This is a huge deal.
- Security – Latest PHP versions offer better security improvements compared to PHP 5, including a filtered un-serialized function and a set of functions to easily get cryptographically secure random numbers.
- JIT (Just-In-Time compilation) – PHP 8 introduced two JIT compilation engines. Tracing JIT, the most promising of the two, shows about 3 times better performance on synthetic benchmarks and 1.5–2 times improvement on some specific long-running applications.
Relative JIT contribution to PHP 8 performance:
- Type system and error handling improvements
including stricter type checks for arithmetic/bitwise operators.
- Scalar type declarations and return type declarations. These features make the maintenance of large pieces of code significantly easier.
- Engine exceptions – with the addition of engine exceptions, fatal errors that would have resulted in script termination can be caught and handled easily.
- Improved argument type declarations, or type hints, allow specifying the type of a variable that is expected to be passed to a function or a class method. Type hints are a feature available since PHP 5, and since PHP 7.2 we can use them with the object data type. Now PHP 7.4 brings type hinting a step forward by adding support for first-class property type declarations.
Modern PHP tools
There are numerous tools that accompany PHP development. However, I will only highlight a few of the most important ones that are considered industry standards.
- Dependency management – Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries.
Composer runs from the command line and installs dependencies (e.g. libraries) for an application. It also allows users to install PHP applications that are available on “Packagist” which is its main repository containing available packages. It also provides autoload capabilities for libraries that specify autoload information to ease usage of third-party code.
Microsoft VS Code IDE – Powerful, cool, and modern code editor from Microsoft that is now hugely popular has native support for PHP. VS Code marketplace also has hundreds of extensions that further optimize PHP development.
- PhpStorm is another hugely popular IDE dedicated solely to PHP development. The editor actually ‘gets’ your code and deeply understands its structure, supporting all the PHP language features for modern and legacy projects. It provides the best code completion, refactorings, on-the-fly error prevention, and more.
- RAY debugging companion is a beautiful, lightweight tool that helps with PHP debugging and that displays debugging info in a very organized way.
The rise of new, cool PHP frameworks
Today we have super cool, powerful, modern PHP frameworks like Symfony and Larevel. Symfony is a more complex, enterprise-oriented framework, while Laravel is equally powerful but more elegant and easier to learn. No wonder, Laravel is now the most popular backend framework.
PHP Larevel quickly became the most popular backend framework:
PHP Larevel is an extremely elegant framework
Laravel is a web application framework with an expressive, elegant syntax. A web framework that provides a structure for creating complex applications. Some notable Laravel features include:
- Elegant syntax – Laravel features expressive, elegant syntax — freeing you to create without sweating the small things.
- Progressive framework – Laravel grows with you. If you’re just taking your first steps into web development Laravel will make it easy. If you’re a senior developer, Laravel gives you robust tools for dependency injection, unit testing, queues, real-time events, and more. Laravel is fine-tuned for building enterprise web applications and ready to handle enterprise workloads.
- Scalable – Laravel is incredibly scalable. Thanks to Laravel’s built-in support for fast, distributed cache systems like Redis, horizontal scaling with Laravel is a breeze. In fact, Laravel applications have been easily scaled to handle hundreds of millions of requests per month.
- MVC pattern – Laravel is largely based on MVC (Model View Controller) architecture.
- Eloquent ORM – Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding “Model” that is used to interact with that table. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well.
A sample taste of Laravel:
PHP has come a long way from the old days of messy, unstructured, and unorganized development. There were attempts to create alternatives which on a large scale proved unsustainable. Instead of throwing PHP into history, it proved more sustainable and reasonable to fix PHP. Now we have a new era of PHP with modernized PHP and cool, modern frameworks and tools.
PHP language has now evolved to streamline Object Oriented Programming, it has great support for dependency management, and strong debugging and testing tools.
Laravel framework makes PHP development elegant, enjoyable, and scalable even for the most complex enterprise applications.