New Features in PHP 8

Two days ago the first “Alpha” of PHP 8 was presented, and PHP 8 is scheduled to release on November 26, 2020, only 5 months from now.

Looking back, I see how previous major releases took PHP to the next levels. I personally only witnessed two of them:

  • PHP 5 essentially revolutionized object-oriented programming in PHP. OOP in PHP 4 was not only insufficient, but also created terrifying number of legacy applications that relied on those deficiencies (I still find those &= in legacy code from time to time). In PHP 5 classes became usable, you could actually design an object-oriented architecture and not think about huge number of limitations (well, at least not as much).
  • PHP 7 took the language a few steps closer to those renowned enterprise-level languages as Java and C#. I’m a huge proponent of static type declarations, and although I appreciate the opportunities dynamic typing provides, it leads to many bugs that are easily preventable by declaring types.

So, What About PHP 8?

This release is going to be huge! There are so many RFC’s already merged and still being discussed, that I can’t wait to begin actually actually using it.

Let’s see what’s out there!

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 further improvement of performance, and indeed very promising according to the tests. Technically, it’s part of the OPcache, but more or less independent from it.

JIT was initially proposed as an experimental feature for PHP 7.4, but didn’t pass the vote, so it was put off until PHP 8. And finally the time has come!

So what is JIT? Simply put, that’s something in between an old-fashioned compiler and good old interpreter. Because of the OPcache, PHP hasn’t been a pure interpreter it used to be for quite a while. Now PHP makes another step towards being a compiler, all while keeping the advantages of an being an interpreter. That’s an odd mix, but I like it a lot!

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 the Late Static Binding, or lack thereof until PHP 5.3 introduced it. Simply put, the special class name self always calls to the class it’s located in, which may be an issue if that class is inherited. To solve that, keyword static was added. You can read about that problem in details in the PHP docs.

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

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

Is That It?

No, not really. There are other less significant new features in PHP 8. Besides that, even more RFC’s are still being drafted, discussed, and voted on, and many of them are too aiming to make it into PHP 8. So we may see even more interesting features by the time of the release.

4 thoughts on “New Features in PHP 8”

Leave a Reply to Sergey Mitroshin Cancel Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.