Do you remember the popular blog post PHP: Poorly Designed Fractals? When I first read it, I was working in a terrible place with a lot of legacy PHP projects. This article made me wonder if I should give up and do something completely different from programming.

Fortunately, I changed jobs soon after, and more importantly, PHP has successfully improved a lot since the 5.x release. Today, I’m paying tribute to those who no longer program in PHP, or are stuck in legacy projects.

Full disclosure: Some things are still bad today, as almost every programming language has its quirks. Many core functions still have inconsistent invocation methods, still have confusing configuration Settings, and still have developers writing crappy code out there because they have to, or because they don’t know any better.

Today I want to look on the bright side: let’s focus on what has changed and how to write clean and maintainable PHP code. Until then, I’d like to ask you to suspend any prejudice for a moment.

Then, you can make fun of PHP just like before. Although, you might be surprised at some of the improvements PHP has made over the years. (LCTT to be honest, I was really surprised)

Look ahead to conclusions

  • PHP is under active development, with new versions coming out every year
  • Performance has doubled, if not tripled, since the PHP 5 era
  • There is a very active ecosystem of frameworks, packages, and platforms
  • PHP has added many new features over the years, and the language continues to evolve
  • Tools like static analysis have matured over the past few years and are constantly growing and updating: people ask me to show some actual code. I think that’s ok! This is the source code of one of my side projects, written in PHP and Laravel; Here’s a list of hundreds of free open source software packages we maintain around the office. Both are good examples of modern PHP projects.

So let’s get started.

Historical summary

For better measurement purposes, let’s take a quick look at today’s PHP release cycle. We are currently at 7.3 in PHP and expect 7.4 by the end of 2019. PHP 8.0 will be the next version after 7.4.

Since the 5.x era, the core team has tried to maintain an annual release cycle, and they’ve managed to do so successfully over the past four years.

Typically, each new release is actively supported within two years and receives a year or more of “security fixes.” The goal is to incentivise PHP developers to stay as up-to-date as possible: for example, it’s easier to make small upgrades every year than to jump between 5.4 and 7.0.

An overview of the ACTIVITIES in the PHP timeline can be found here.

Finally, PHP 5.6 is the latest 5.x release, while 8.0 is currently the next big release. If you want to know what’s going on in PHP 6, you can listen to the PHP Roundtable podcast.

With that in mind, let’s debunk some common misconceptions about modern PHP.

The performance of the PHP

Back in the 5.x era, PHP represented… Well, average. But in version 7.0, PHP was rewritten from the ground up, resulting in a two – or three-fold improvement in performance!

But words are not enough. Let’s look at benchmarks. Fortunately, a lot of time has been spent benchmarking PHP performance. I found that Kinsta has a nice updated list of tests.

Since the 7.0 upgrade, performance has been improving without backtracking. The performance of PHP Web applications is comparable to, and in some cases better than, Web frameworks in other languages. You can check out this extensive benchmarking suite.

PHP frameworks don’t beat C and Rust, of course, but they are much better than Rails or Django, and are comparable to ExpressJS.

Frameworks and ecosystems

Speaking of frameworks: PHP is more than WordPress. Let me tell you something — some professional PHP developers: WordPress is by no means representative of the modern PHP ecosystem.

In general, there are two major Web application frameworks, Symfony and Laravel, as well as several smaller application frameworks. Of course there are Zend, Yii, Cake, Code Igniter, etc., but either one is a good choice if you want to know what modern PHP development is like.

Both frameworks have a huge ecosystem of packages and products. From management panels and CRM to standalone packages, from CI to analyzers, as well as several Web socket servers, queue managers, payment integration and many more. To be honest, it’s a lot to list.

These frameworks, though, are suitable for real-world development. If all you need is a content management system (CMS), platforms like WordPress and CraftCMS will do.

One way to gauge the current state of the PHP ecosystem is to look at Packagist, PHP’s main package repository. It is now growing exponentially. With 25 million downloads per day, the PHP ecosystem is no longer the small underdog it once was.

Take a look at this chart, which lists the number of packages and versions over time. It can also be found on the Packagist website.

In addition to application frameworks and CMS, we’ve seen the rise of asynchronous frameworks in the past few years.

These are frameworks and servers written in PHP or other languages that allow users to run true asynchronous PHP. Examples include Swoole (founder Han Tianfeng), and Amp and ReactPHP.

We’ve moved into the asynchronous world, and things like Web sockets and applications with lots of I/O have become very important in the PHP world.

The internal mailing list (where PHP core developers discuss language development) already talks about adding libuv to the core. If you don’t already know, libuv: Node.js is all about providing asynchrony.

The language itself

While async and await are not yet available, the PHP language itself has improved a lot over the past few years. Here’s a non-exhaustive list of the new features in PHP:

  • Short closure (arrow function)
  • The Null merge operator(??)
  • Traits (a code reuse approach)
  • Attribute types
  • Expansion operatorParameter unpacking.)
  • JIT compiler (just-in-time compiler)
  • FFI (External Function Interface)
  • An anonymous class
  • Return type declaration
  • Modern encryption support
  • The generator
  • , etc.

When we talk about language features, we’ll also talk about the evolution of that language today. While the community can come up with an RFC, it needs an active core of volunteers to push it forward.

Next, these RFCS are discussed in an “internal” mailing list, which can also be read online. A vote is required before new language features can be added. Only RFCS approved by at least a two-thirds majority can enter the core.

There may be about 100 people who can vote, but you don’t need everyone to vote on every RFC. The members of the core team can certainly vote; they are the ones who maintain the code base. In addition to them, a group of people were singled out from the PHP community. These include maintainers of PHP documentation, people who contribute to the PHP project as a whole, and prominent developers in the PHP community.

While most of the core development is done on a voluntary basis, one of the core PHP developers, Nikita Popov, was recently hired by JetBrains to work full-time on the PHP language. Another example is the Linux Foundation’s recent decision to invest in the Zend framework. Hires and acquisitions like these ensure the stability of PHP’s future.

tool

In addition to the core itself, we’ve seen an increase in tools around it over the past few years. The first ones that come to mind are static parsers like Psalm created by Vimeo, Phan, and PHPStan.

These tools will statically analyze your PHP code and report any types of errors, possible errors, etc. To some extent, they provide functionality comparable to TypeScript, but the current language does not translate transpiling and therefore does not support the use of custom syntax.

Although this means relying on Docblocks, the father of PHP, Rasmus Lerdorf, did mention the idea of adding a static analysis engine to the core. There is a lot of potential, but it is a difficult task.

Speaking of translation, and being inspired by the JavaScript community; They have made an effort to extend PHP syntax in the user domain. A project called Pre does just that: it allows you to translate new PHP syntax into plain PHP code.

While this idea is already proven in the JavaScript world, it can work in PHP if you provide the appropriate IDE and static analysis support. It’s a very interesting idea, but it has to be developed to be considered “mainstream”.

conclusion

Still, you can think of PHP as a terrible language. While the language certainly has its drawbacks and 20-year legacy; But I can safely say THAT I enjoy working with it.

Based on my experience, I am able to create reliable, maintainable, and high-quality software. The clients I work for are happy with the end result, “me too”.

Although you can still do a lot of messy things with PHP, I think it’s a great choice for Web development if used wisely and correctly.

Don’t you agree? Let me know why! You can reach me on Twitter or email.


Via: stitcher. IO/blog/PHP – in…

By Brent, lujun9972

This article is originally compiled by LCTT and released in Linux China