PHP 8 in eight pieces of code

PHP 8 has a lot of new features, in this list we will look at the most outstanding.

Disclaimer: The link to this article in English was already in the post of the last PHP digest. If you know the language enough, perhaps you should go to the original, there are a lot of footnotes to other English-language articles.

Time


use \Support\Attributes\ListensTo;

class ProductSubscriber
{
    <<ListensTo(ProductCreated::class)>>
    public function onProductCreated(ProductCreated $event) { /* … */ }

    <<ListensTo(ProductDeleted::class)>>
    public function onProductDeleted(ProductDeleted $event) { /* … */ }
}

Instead of docblock, attributes.

Yes, I know, the syntax may not be the way you wanted or hoped. Perhaps you would prefer @, @ :, or docblocks, or something else. But he will be just that. The only thing worth mentioning about the syntax is that all the options have been discussed, and there are very good reasons why this syntax was chosen. You can read a brief summary of this in the RFC , or you can read the entire discussion about RFC in the list of internal components .



Two


public function foo(Foo|Bar $input): int|float;

public function bar(mixed $input): mixed;

Combining types allows you to specify several types at once. There is also a new mixed type that represents several types at the same time.



Three


interface Foo
{
    public function bar(): static;
}

You can specify static in the return type.



Four


[JIT]
opcache.jit=5

Built-in JIT compiler.

What is JIT?

"JIT" stands for "just in time" - "at the right time." You probably know that PHP is an interpreted language: it is not compiled as a program in C, Java, or Rust. Instead, it translates into machine code — something that the processor understands — at runtime.

"JIT" is a method that compiles portions of code at runtime so that a compiled version can be used instead.

Think of it as a “cached version” of interpreted code generated at runtime.

In more detail about it already wrote on Habré



Five


$triggerError = fn() => throw new MyError();

$foo = $bar['offset'] ?? throw new OffsetDoesNotExist('offset');

throw can be used in expressions.



Six


try {
    // Something goes wrong
} catch (MySpecialException) {
    Log::error("Something went wrong");
}

You can omit the exception variable if you do not need it.



Seven


public function(
    string $parameterA,
    int $parameterB,
    Foo $objectfoo,
) {
    // …
}

You can specify a comma at the end of the parameter list.



Eight


str_contains('string with lots of words', 'words');

str_starts_with('haystack', 'hay');

str_ends_with('haystack', 'stack');

New string functions. I think their name speaks for itself.
Let's not fool ourselves: 8 blocks of code are not enough to summarize all the great new things in PHP 8. So, let's just add a few more.



Nine


function bar(Stringable $stringable) { /* … */ }

A new Stringable interface that is automatically added to classes that implement the __toString () method.



Ten


$object::class

Calling :: class directly from the object.



All Articles