Scroll to top
© 2020 OCTOCODE

What’s New in PHP 8?


octocode - May 6, 2021 - 0 comments

December 2020 is when PHP 8 will be released to the world. If you are wondering what new  features the language developers have come up with, this post is for you. I chose eight, in my  opinion, the most interesting features that we will get with the new version. 

1. just in time 

JIT (Just in Time) means that the code of your application will be compiled only when needed.  Suppose you create function which will not be called during application launch. Then it will not be  compiled. 

Thanks to that we will save precious server resources and time needed for compilation. What is  interesting using JIT is supposed to be possible for both new and old scripts. Does it mean that we  are waiting for speeding up for example websites based on WordPress? A lot points to it, but we will see how it will be. 

2. union types 

In all places where we can define value types, we will be able to use union types. In practice it  means that the variable will be able to be of type int or float. Of course everything depends on what  we need at the moment. The principle is simple, we use the notation T1 | T2 in place of defining the  type. In code, we can write it like this: 

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

Despite declaring support for all types available today, we have a few caveats. 

1. The void type cannot be included in union types. So writing for example int|void will cause  an error. 

2. If a variable is to be nullable you can write it as int|null 

3. Duplicate or redundant types will cause compilation errors. Below example illustrates it best 

function foo(): int|INT {} // Disallowed
function foo(): bool|false {} // Disallowed
 
use A as B;
function foo(): A|B {} // Disallowed ("use" is part of name resolution)
 
class_alias('X', 'Y');
function foo(): X|Y {} // Allowed (redundancy is only known at runtime)

3. no more unnecessary typing 

Do you associate the situation where you pass a value to the constructor to later assign it to a private variable? 

class Point {
    public float $x;
    public float $y;
    public float $z;
 
    public function __construct(
        float $x = 0.0,
        float $y = 0.0,
        float $z = 0.0,
    ) {
        $this->x = $x;
        $this->y = $y;
        $this->z = $z;
    }
}

No more! From now on you can do the exact same job using only the constructor. Enjoying the little things.

class Point {
    public function __construct(
        public float $x = 0.0,
        public float $y = 0.0,
        public float $z = 0.0,
    ) {}
}

4. back to types 

There will be a new type mixed, which can be used as a replacement for union types. The  developers claim that this type will allow more precise indication of methods that accept / return  any type of value. 

This might be a good idea to maintain some sort of consistency in the code. However, it does not  seem to be a rocket feature. In code, it will present itself as follows: 

function test(mixed $params): mixed {
   //... smthg
}

5. take my class 

Have you ever used the get_class() function? I’m sure you have. PHP 8 comes with a competitive  way to check the class of an object. 

Just call the static ::class property and you will get the class of the object. 

$object = new stdClass;
var_dump($object::class); // "stdClass"
 
$object = null;
var_dump($object::class); // TypeError

6. Exceptions 

We still need to define a variable if we want to catch an exception. Even if we do not use this  variable anywhere. 

try {
    foo();
} catch (SomeException $ex) {
    die($ex->getMessage());
}

In 8th version, this will not be necessary. It is sufficient to declare a catch. 

try {
    changeImportantData();
} catch (PermissionException) { // The intention is clear: exception details are irrelevant
    echo "You don't have permission to do this";
}

7. new interface 

Relax, it’s not about the UI 🙂 It’s about the new built-in Stringable interface. What’s interesting is that if you want your object to implement this interface, it only needs to include implementations of the __toString() method. You don’t have to explicitly mark the Stringable implementation when you declare the class, but you can do so. 

With this interface, we will be able to use the following notation in union types string | Stringable.  That is, either a string or something that can be read as a string. 

8. nullsafe operator 

You want to call a method / property, but you’re not sure if you’re calling it on something that is  null? Right now you’re probably doing it this way: 

$country =  null;
 
if ($session !== null) {
    $user = $session->user;
 
    if ($user !== null) {
        $address = $user->getAddress();
 
        if ($address !== null) {
            $country = $address->country;
        }
    }
}

If you use PHP 8 you can do this in a much more convenient way: 

$country = $session?->user?->getAddress()?->country;

If at any stage something returns null, the whole thing will return null. Otherwise, we’ll go to the  last call. 

That’s all for today. I hope you found this article useful and that you are waiting for the new version 

of PHP as much as I am. It can be really interesting. Remember also that you can find all the  changes in the new version on the official website https://wiki.php.net/rfc#php_80.

Related posts

Post a Comment

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