Two days ago, the first “Alpha” of PHP 8 was presented, and PHP 8 should be released on November 26, 2020, in just 5 months.

Looking back, I see how the previous major versions took PHP to the next levels. Personally, I have only seen two:

  • PHP 5 has essentially revolutionized object oriented programming in PHP. The OOP in PHP 4 was not only insufficient, but also created a terrifying number of legacy applications that relied on these deficiencies (I still find these in legacy code from time to time). In PHP 5, classes have become usable, you can actually design an object-oriented architecture and not think of a large number of limitations (well, at least not as much).
  • PHP 7 brought the language closer to these professionally renowned languages such as Java and C #. I’m a big fan of static type declarations, and although I appreciate the opportunities offered by dynamic typing, this leads to many bugs that can be easily avoided by declaring types.

 

So, What About PHP 8?

This version is going to be huge! There are so many RFCs already merged and under discussion, that I can’t wait to start using it.

Let’s see what exists!

 

Constructor Property Promotion

This syntax allows you to declare your properties right in the construction arguments:

 

class Point {
    public function __construct(
        public float $x = 0.0,
        public float $y = 0.0,
        public float $z = 0.0,
    ) {}
}

 

While I feel uneasy about this particular feature, it’s still a nice syntax sugar to have. Unfortunately, it can be easily abused and make the code a little bit messier.

 

Just-In-Time Compiler

This is a new performance improvement, and indeed very promising according to the tests. Technically, this is part of the OPcache, but more or less independent of it.

JIT was originally proposed as an experimental feature for PHP 7.4, but was not voted, so it was postponed to PHP 8. And finally, the time has come!

So what is JIT? In other words, it’s something between an old-fashioned compiler and a good old performer. Because of the OPcache, PHP was not as pure a performer as it had been for a long time. Now PHP is one step closer to compiling, while retaining the advantages of being an interpreter. It’s a strange mix, but I love it!

 

Union Types

You no longer have to limit yourself to one particular type, you can specify many:

 

public function squarePositive(int|float $number): string|false {
    return $number > 0 ? sqrt( $number ) : false;
}

 

 

Attributes

These are going to replace PHPDoc syntax for describing some technical information. That’s a good thing, because PHPDoc is for documentation, and not for fields of your database (looking at you Doctrine!).

 

<<ExampleAttribute>>
class Foo {
    <<ExampleAttribute>>
    public function foo(<<ExampleAttribute>> $bar) {}
}

 

However, unlike other feature, this one is less certain. There’s currently happening a vote to reconsider the syntax, so it might look a little bit different in the final version. However, the attributes are here to stay, that’s for sure.

 

New functions: str_starts_with() and str_ends_with()

The names are speak for themselves: they allow you to see if the beginning of the end of the string matches your value:

 

str_starts_with ( string $haystack , string $needle ) : bool;
str_ends_with ( string $haystack , string $needle ) : bool;

 

New function: str_contains()

What do you do if you need to see if a string contains a substring? Most likely you’ll use strpos(). And what if your substring is in the beginning of your string? Well, that may lead to hard-to-catch bugs:

 

if (strpos('some string', 'some')) {} // this condition is falsy
if (false === strpos('some string', 'some')) {} // that's how you should do it!

 

The new function str_contains() returns a boolean value, so you no longer have to worry about it.

 

Non-Capturing Catches

You don’t always need the exception object when you catch it. But you were forced to save the object anyway. Well, no more:

 

try {
    changeImportantData();
} catch (PermissionException) {
    echo "You don't have permission to do this";
}

 

Mixed Type

If your function argument can be of virtually any type, but you want to declare its type anyway, you can now use mixed:

public function foo(mixed $value) {}

To be more precise, mixed is a shorthand for:
array|bool|callable|int|float|null|object|resource|string.

 

New Stringable Interface

This interface is automatically added to any class that implements __toString() method. This means that you now can write code like this:

public function doSomething(string|Stringable $str) {}

And that makes a lot of sense, because you can use any such object as an actual string.

 

Return Type static

This goes back to late static binding, or lack of it until PHP 5.3 introduced it. In other words, the special class name self always calls the class it is in, which can be a problem if this class is inherited. To resolve this problem, a static keyword was added. You can read this problem in detail in PHP documents.

In PHP 8, you can specify static as the method return type:

public function doSomething(): static {
    return new static();
}

 

Is That It?

Not really. There are other, less important new features in PHP 8. On top of that, even more RFCs are still being written, discussed, and voted on, and many of them are too focused on making it PHP 8. So we can see even more interesting features at the time of release.

Leave A Comment

Whatsapp Whatsapp Skype