⬆️ ⬇️

PHP 2019: Better Than You Think About It





Remember the once popular publication: “ PHP: a fractal of bad design ”? When I first read it, I worked in a stupid place with a large number of obsolete PHP projects. She made me think: should I leave and do something completely different than programming.



Fortunately for me, I was soon able to change jobs, and, more importantly, PHP has managed to "develop a little" since the 5. * versions. And today, through this article, I want to appeal to people who either no longer program in PHP, or are stuck in obsolete projects.



Spoiler: today PHP, like almost every programming language, is full of flaws. Many basic functions still have inconsistent signatures, configuration settings are confusing, still full of developers writing a crappy code, because they don’t work differently in their current project or because they can’t do otherwise.

')

Today I want to look at the bright side: let's focus on what has changed and on how to write clean and supported PHP code. I want to ask you to drop prejudices for a few minutes.



After that, you can think about PHP in the same way as before. Although, most likely, you will be surprised at some of the improvements made in PHP over the past few years.



Tl; DR





Short story



Briefly consider the release cycle of PHP. Now the current version is PHP 7.3, and version 7.4 is expected at the end of 2019. PHP 8.0 will be the next version after 7.4. Since the late era of PHP 5. *, the team of maintainers has been trying to maintain an annual release cycle and for the past four years she has been doing well.



In general, each new release is actively maintained for two years and receives one more year of “security fixes”. The goal is to motivate PHP developers to keep the version up-to-date: applying small updates every year is much easier than, for example, switching from 5.4 to 7.0.



An updated calendar of dates for support of different versions of PHP can be found here .



Finally, PHP 5.6 was the latest release of branch 5. *, and 7.0 was next. If you want to know what happened to PHP 6, you can listen to the PHP Roundtable podcast .



Now let's debunk some common misconceptions about modern PHP.



PHP performance



On days of version 5. * PHP performance was ... average, at best. However, in version 7.0, significant portions of the PHP core were rewritten from scratch, resulting in a performance increase of two to three times.



But words are not enough. Let's look at the tests. Fortunately, other people have spent a lot of time benchmarking PHP performance. I believe that Kinsta has a good updated list.



Starting with version 7.0, productivity only increases. So much so that PHP web applications have comparable, and in some cases, better performance than web frameworks in other languages. Take a look at this extensive test suite.



Of course, PHP frameworks will not exceed C and Rust, but they are much better than Rails or Django, and comparable to ExpressJS.



Framework and Ecosystem



Speaking of frameworks: PHP is no longer limited to WordPress. I dare to tell you as a professional PHP developer: WordPress is in no way a representative of the modern ecosystem.



In general, there are two main web application frameworks: symfony and Laravel . And a few smaller ones: Zend, Yii, Cake, CodeIgniter, etc. But if you want to know what modern PHP development looks like, a good choice would be to meet one of the two largest.



Both frameworks (Symfony and Laravel) have a large ecosystem of packages and products. Starting from administrative panels and CRM to stand-alone packages (in the original “standalone packages”), from CI to profilers, as well as numerous services, such as web socket servers, queue managers, payment integration; frankly, too many to list all.



However, these frameworks are designed for immediate development. If you just need content management, platforms like WordPress and CraftCMS are just getting better and better.



One way to measure the current state of the PHP ecosystem is to look at packagist, the main package repository for PHP. It grows exponentially. With ~ 25 million downloads per day, it is fair to say that the PHP ecosystem is no longer the weak side it used to be.



Look at this graph, which shows the number of packages and versions over time. It can also be found on the packagist website .







In addition to application frameworks and CMS, in recent years we have also witnessed the growth of asynchronous frameworks.



These frameworks and servers written in PHP or other languages ​​allow the use of truly asynchronous PHP. Examples include Swoole , Amp and ReactPHP .



Since we entered the asynchronous world, things like web sockets and applications with lots of I / O operations have become truly relevant in the PHP world.



The internals mailing list - the place where leading developers discuss language development - also said to add libuv to the kernel. For those who do not know libuv, this is the same library that Node.js uses to provide all its asynchrony.



Language itself



Although async and await are not yet available, in recent years there have been many improvements in the language itself. Here is an incomplete list of new features in PHP:





While we are talking about language features, let's also touch on the topic of how language is developing today. There is a team of volunteer maintainers who move the language forward, and the community can also offer RFCs.



These RFCs are then discussed on the internals mailing list, which can also be found on the Internet. Before a new language feature is added, it is necessary to vote. Only RFCs with a majority of votes not less than ⅔ of all those who voted are allowed to be included in the core.



About 100 people take part in the voting, while the requirements to vote at each RFC are not. The group of people who have the opportunity to vote, of course, includes the maintainers, since they must maintain the code base. In addition to them, there is a group whose members were individually selected from the PHP community. This group consists of PHP documentation maintainers, authors of major PHP projects, and outstanding developers from the PHP community.



Although most of the development takes place on a voluntary basis, one of the developers of the PHP core, Nikita Popov, was recently hired by JetBrains to work on the language for a full-time job. Another example is the Linux Foundation, which recently decided to invest in the Zend Framework. Such work and acquisitions guarantee stability for the future of PHP and its development.



Tools



In addition to the core itself, over the past few years we have seen an increase in tools around it. Psalm static analyzers created by Vimeo, Phan and PHPStan come to mind .



These tools statically analyze your PHP code and report any type errors, possible bugs, etc. In some sense, the functionality they provide can be compared to TypeScript (translator’s note: static analyzers extend the language’s ability to find errors / flaws) while improving the language, TS conditionally does the same on top of JS) , although at the moment PHP is not transpilable, so custom syntax is not allowed.



Although we need to rely on docblocks and typing while doing this, Rasmus Lerdorf, the creator of PHP, mentioned the idea of ​​adding a static analysis mechanism to the kernel. This task contains a lot of potential, but it is huge in terms of labor costs.



Speaking of transpiling, it is worth noting that there were attempts to extend the PHP syntax not at the kernel level, but at the level of user libraries, as implemented in JavaScript. A project named Pre does just that: it allows you to use the new PHP syntax, which is transferred to regular PHP code.



Although this approach has proven itself in the JavaScript world, it will only be able to make money in PHP if proper support for IDE and static analysis is provided. This is a very interesting idea, but it still has a long way to go before it can be called “mainstream”.



Finally



Despite everything said, feel free to think of PHP as a terrible language. He certainly has his flaws and a 20-year legacy, but I can confidently state that I like working with him.



With it, I can create reliable, well-maintained and high-quality software. The clients I work for are satisfied with the end result, just like me.



Despite the fact that when working with PHP you can still write very bad code, I would say that this is a great choice for web development if you use it correctly.



You do not agree? Write in the comments why!

Source: https://habr.com/ru/post/456024/



All Articles