DEV Community

Brent Roose
Brent Roose

Posted on • Originally published at stitcher.io on

PHP 8 in 8 code blocks

PHP 8 brings lots of new features, in this list we'll look at the most outstanding ones. If you want a full list and background information, you can read about all things new in PHP 8.


use \Support\Attributes\ListensTo;

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

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

Attributes — aka annotations — you can about them in depth in this post.


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

public function bar(mixed $input): mixed;

Union types allows for type hinting several types. There's also a new mixed type which represents several types at once.


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

The static return type is built-in.


[JIT]
opcache.jit=5

The just-in-time compiler for PHP.


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

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

throw can be used in expressions.


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

Non-capturing catches: no need to specify an exception variable if you don't need it.


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

Trailing commas are allowed in parameter lists


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

str_starts_with('haystack', 'hay');

str_ends_with('haystack', 'stack');

New string functions.


Let's not fool ourselves: 8 code blocks isn't enough to summarise all great new things in PHP 8. If you want a full list, you can find it on my blog.

What feature are you looking forward to the most? Let me know on Twitter or via email.

Latest comments (1)

Collapse
 
moopet profile image
Ben Sinclair

This is the first I've seen of these, and I worry that they're... well, they're not all good things. For one thing, there's a lack of simple progression: you can't look at them all with knowledge consisting mostly of previous PHP syntax and guess what they'll do.

Attributes are probably a good thing. I mean, anything to get away from the practice of annotating things with comments, which is used by a bunch of frameworks at the moment. Shudder.

"mixed" means... well, something. Is it there just to take up space, because the parser now requires a type? That's fine, but the linked explanation suggests it can be null but can't be nullable and that it is there because, for one thing, Bad Old PHP might have returned null instead of a known type. That means nothing to me.

"static" is already overloaded. It's getting another use? Cool. Why not. YOLO.

"throw can be used in expressions." - I didn't realise it couldn't. I think that writing exceptions into expressions is Straying Too Far From The Path, though, so I probably never tried.

"Non-capturing catches"? Reasonable.

"Trailing commas are allowed in parameter lists"? Good, I guess. It's a non-breaking update that will make it more consistent with arrays.

"New string functions". Oh come on, PHP. Stop adding millions of inconsistent functions that are there to replace... well, other functions. This is already way too sugary.