What's new in PHP8?

November 15, 2020 ≈ 1 minute 48 seconds

PHP8 brings a lot of new features like named parameters, Nullsafe operator, Trailing comma in parameters, Catch without variable, Class name literal on an object, and much more. Here are examples of how these new PHP8 features work.

<?php

class Subscriber {
    public function getContacts(): Contacts|null
    {
        // return new Contacts();
        return null;
    }
}

class Contacts {
    public function getAddress(): string
    {
        return 'address...';
    }
}

$subscriber = new Subscriber();
echo $subscriber->getContacts()?->getAddress();

Nullsafe operator

<?php

try {
    throw new UnexpectedValueException();
} catch (Throwable) {
    echo 'caught...';
}

Catch without variable

<?php

class Subscriber {
    function __construct(
        public string $name,
        public string $email,
    ) {}
}

$new_subscriber = new Subscriber(
    'Andrew',
    'some_random_mail@some_random_mailer.dev',
);

var_dump($new_subscriber);

Constructor property promotion and trailing commas

<?php

//null ?? throw new UnexpectedValueException();
false ?: throw new UnexpectedValueException();

Throw as an expression

<?php

class Amount {
    private int|float|null $amount;

    public function setAmount(int|float|null $amount)
    {
        $this->amount = $amount;
    }

    public function getAmount(): int|float|null
    {
        return $this->amount;
    }
}

$amount = new Amount();
$amount->setAmount('oh no...');
echo $amount->getAmount();

Union types

<?php

function subscribe(string $name, bool $active = false, string $email) {}
subscribe(name: 'Andrew', email: 'some_random_mail@gmail.com');

Named parameters

<?php

var_dump(str_contains('Andrew', 'drew')); // true

New built-in function str_contains

<?php

function test(mixed $everything) {
    var_dump($everything);
}

test(false);

New mixed type

<?php

try {
    strpos("Andrew", "drew", 7);
} catch (ValueError) {
    // PHP Fatal error:  Uncaught ValueError: strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
    echo 'value error...';
}

Value errors

<?php

echo str_starts_with('Andrew', 'And'); // true
echo str_ends_with('Andrew', 'drew'); // true

Built-in functions str_starts_with and str_ends_with

<?php

class Subscriber {
    public function __toString(): string
    {
        return 'subscriber';
    }
}

$subscriber = new Subscriber();
var_dump($subscriber instanceof Stringable); // bool(true)

New Stringable interface

<?php

#[\Attribute]
class SomeAttribute {
    public function __construct($message)
    {
        echo $message;
    }
}

#[\SomeAttribute('some message')]
class AttributeTest {
}

$attribute_test = new AttributeTest();

$reflection_class = new ReflectionClass($attribute_test);
$attributes = $reflection_class->getAttributes();

$attributes[0]->newInstance(); // some message

Attributes

<?php

class Subscriber {}

$cache = new WeakMap();
$subscriber = new Subscriber();

$cache[$subscriber] = ['some', 'cached', 'data'];
echo $cache[$subscriber][0]; // some
var_dump(count($cache)); // int(1)

unset($subscriber);
var_dump(count($cache)); // int(0)

Weak maps

<?php

class Hello {}

$hello = new Hello();
echo $hello::class;

Class name literal on object

#php

Subscribe to our newsletter

If you provide url of your website, we send you free design concept of one element (by our choice)

Subscribing to our newsletter, you comply with subscription terms and Privacy Policy