tech · 29 Dec 2020

PHP 8.0 Cheat Sheet: Summary of New Features

Aleksandr Denisyuk

Aleksandr Denisyuk

PHP Developer

new in php 8
Illustration by Amir Kerr

This is a summary of the most important future-proof features of PHP 8.0. Use this cheat sheet to ensure better code design and make future project updates easier.

What's New in PHP 8.0

1. Added named arguments which allow passing arguments to a function based on the parameter name, rather than the parameter position:

array_fill(0, 100, 50);
 
// or
 
array_fill(value: 50, num: 100, start_index: 0);

2. Added attributes supported, which allow adding structured, machine-readable metadata information on declarations in code: classes, methods, functions, parameters, properties, and class constants can be the target of an attribute:

#[Attribute]
class Metadata
{
    public mixed $value;
 
    public function __construct(mixed $value)
    {
        $this->value = $value;
    }
}
 
#[Metadata(value: 42)]
class Thing
{
}
 
$reflection = new ReflectionClass(Thing::class);

/** @var \ReflectionAttribute[] $attributes */
$attributes = $reflection->getAttributes();
 
foreach ($attributes as $attribute) {
    $attribute->getName(); // Metadata
    $attribute->getArguments(); // ['value' => 42]
}

3. An optional trailing comma is now allowed in parameter lists:

function foo(
    int $foo,
    string $bar,
) {
    // ...
}

4. Added constructor property promotion. When a constructor argument includes a visibility modifier, PHP will interpret it as both an object property and a constructor argument and assign the argument value to the property:

class Point
{
    public function __construct(
        public int $x,
        public int $y = 0,
    ) {}
}

5. A union type declaration accepts values of multiple different types, rather than a single one:

class Number
{
    public function __construct(
        private int|float $number,
    ) {}
 
    public function getNumber(): int|float
    {
        return $this->number;
    }
}

6. Added match expression which based on an identity check of a value:

$condition = 2;
 
$result = match ($condition) {
    1 => 'foo',
    2, 3 => 'bar',
    default => 'xyz',
};

echo $result; // bar

7. Added ?-> nullsafe operator:

/** @var string|null $userName */
$userName = $repository?->getUser(5)?->name;

8. static can now be used as a return type:

class Foo
{
    public function bar(): static
    {
        return new static();
    }
}

9. It is now possible to fetch the class name of an object using $object::class. The result is the same as get_class($object):

$object = new \stdClass();

$object::class; // stdClass

10. Added mixed type

11. Added Stringable interface

12. Added WeakMap class

13. It is now possible to write catch (Exception) to catch an exception without storing it in a variable:

try {
    throw new \Exception();
} catch (Exception) {
    // ...
}

14. throw can now be used as an expression:

$fn = fn() => throw new Exception(); 

// or

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

15. Added create DateTime objects from interface:

$dt = DateTime::createFromInterface(
    DateTimeInterface $other
);

16. Added fdiv(), get_resource_id() functions

17. Added get_debug_type(), token_get_all() functions

18. Added str_contains(), str_starts_with(), str_ends_with() functions for string:

str_contains('ob', 'foobar'); // true
str_starts_with('foo', 'foobar'); // true
str_ends_with('bar', 'foobar'); // true

19. Added ValueError exception

20. The @ operator no longer silences fatal errors

Rate this article!

(6 ratings, average: 5 out of 5)