php-logo

My private look on PHP8 and most interesting features

For starters, let’s say something about PHP’s history. I won’t start from the beginning, because it is too far from PHP8. I will better take a look at what, in my opinion, made people to actually do not respect PHP.

It all started not because PHP was at first, template language, but because what came with PHP4. It was the very same moment when PHP was first called a programming language. However, along with this version came WordPress and other simillar solutions. Code wasn’t clean, plugins were and still are written without enough knowledge of best practices. What’s more there was not a proper oop paradigm implementation at that time. It was started about PHP5.3 and a real, final form of it was introduced in PHP 7.0. Going further, there are just 5 subversions of PHP 7. So, after adding it all up, you can see the perspective of a language which was strange and much more vulnerable just because of its transformation and bad usage rather than a real malfunction.

PHP7 as a real deal-breaker

As I mentioned above, the biggest change in PHP was introduced as of 7.0 version. For instance, there were more like a lot of deprecations were removed what makes old code inexecutable. A lot of security flaws were fixed. It is also much more performant because of low level optimization (done on level of assembler). A lot about it is said in this video. To sum up, PHP 7 is a real intermediate step to something even bigger, if you look at it in a correct perspective.

Let’s talk about PHP8 features…

Most important features of PHP8, which are accepted in RFC document for it, are:

  • JIT,
  • union types,
  • nullsafe operator,
  • Attributes,
  • Match Expression,
  • “mixed” type,
  • Weak maps.

These are just few changes of all proposed in RFC for PHP8. I mentioned those, because I see them as those most interesting as of they stick to performance – at runtime and at pre-compilation time.

Yes, I used “pre-compilation” term, as of the fact CGI was “interpreting” PHP, but fpm is interpreting, pre-compiling it and then it executes it. To speed up all this process they want to add JIT so there will be no need to buffer it, it will pre-compile in a matter of seconds in real time.

Union types

Union types are kind of language structure allowing a programmer to define strict types for returned value of a method or its arguments. It is not exactly the same as mixed, but it can be, because there is no (known) limitation of how many types can be included in an union. In my opinion, it can be good as well as vulnerability in a field of clean code. I think that, because a programmer will be able to allow many types. Not just a couple of them, but three or even four what, even if explicitly mentioned, can be disruptive.

Nullsafe operator

This operatror is somehow simillar to null coalescing operator. It worked on variable calls, but doesn’t work for method calls. And this is what nullsafe operator is designed for: to add support for nullsafe method calling. This is known from Java or C#, but those languages are strong typed from the very beginning. In case of PHP, I believe it will be good functionality. However, a programmer will be in a need of thinking if it doesn’t add too much noise to the code.

Attributes

This structure is also known as annotation in languages such as Java. It is used, to modify interpreters behaviour in different scopes i.e. class scope or method scope, but also in different retention: Runtime, Interpreting etc. It is just good, attributes allow to shorten code extremley without any friction that it will get unmanagable.

Match Expression

There is a switch structure which is often not used because a lot of programmers tries to do “a ladder of if/else statements” what makes code very long and unoptimised. However, match expression will implement exactly the same logic as if you would use switch statement but it will be much more clean and shorter than before. In my opinion, it is good for code’s quality as well as for a programmer, because it is more familliar for PHP developer – array-like syntax.

“Mixed” type

It was not really in my understaing why in PHP7.2 I couldn’t use “mixed” keyword nowhere else than in a phpdoc variable description. “Mixed” alone means totally nothing. It can be anything – null, nothing and any other type you can imagine. So, from now on, mixed type will represent strict list of types:

  • array
  • bool
  • callable
  • int
  • float
  • null
  • object
  • resource
  • string

What’s more, “mixed” keyword will be added so it can be used in method signatures and as a return type of methods. In my opinion, this change is only for good – makes “mixed” type much more organized, makes its usage less unpredictable. Code will be less error prone for sure, but still will be backwards compliant.

Weak Maps

Built upon the weakrefs RFC that was added in PHP 7.4, a WeakMap implementation is added in PHP8. WeakMap holds references to objects, which don’t prevent those objects from being garbage collected. As you can imagine, it will allow ORMs to better manage for example cached structures. If those will be only accessed by cache, the GC will be allowed to clean them up. This is definitely for good, despite the fact whole codebase using old structures will need to be rewritten. However, the performance will be much, much better than it was.

To sum up, all of those changes are good in some way – a lot of them optimise PHP and its runtime, but some will allow to add more noise to code i.e. inproper use of union types will make simillar problems as appeared in case of mixed before. But all to all, I think all is going in a right direction, but it is uinfortunately too late. I have written down here facts as well as my own opinions – this must be taken with a pinch of salt, because I might be mistaken. I am just a human, aye?

If you think, this was good, but this is first of my posts, check out others, beginning from my starting page.

Thanks for reading and see you here next time!
Greetings folks!

Leave a Comment

Your email address will not be published. Required fields are marked *