DEV Community 👩‍💻👨‍💻

Cover image for PHP8.2: the modern PHP
bga
bga

Posted on

PHP8.2: the modern PHP

PHP8.2 adds a lot of new features and improvements such as readonly classes, stand-alone types(null, true, false), Disjunctive Normal Form(DNF) types, new random extension, new classes, interfaces, and functions, and also deprecations some functions and classes.

Readonly Classes

PHP 8.1 added support for readonly properties. A readonly property can only be set once, and PHP actively prevents the value from being modified even from within the scope of the class.

PHP 8.2 takes readonly properties a step further with readonly classes. When a class is declared as readonly, all of its properties are automatically declared readonly. Further, it prevents dynamic properties on readonly classes, and ensures that all properties are typed.

Prevents dynamic properties on Readonly classes

readonly class Blog {
    public string $title;
}

$blog = new Blog();
$blog->subtitle = 'Hello';
Enter fullscreen mode Exit fullscreen mode

The above will result in:
Error: Cannot create dynamic property Blog::$blog...

Ensures all properties are typed

readonly class Blog {
    public $title;
}
Enter fullscreen mode Exit fullscreen mode

Fatal error: Readonly property Blog::$title must have type...

Example

readonly class ExampleBlog {
    public string $myTitle;
}
Enter fullscreen mode Exit fullscreen mode

N.B: Abstract classes and final classes can also be readonly.

Disjunctive Normal From(DNF) Types

DNF types allow us to combine union and intersection types, following a strict rule:

when combining union and intersection types, intersection types must be grouped with brackets.

Previously, in PHP < 8.2:

class Blog {
    public function checkEntity(mixed $entity) {
        if ((($entity instanceof BlogA) && ($entity instanceof BlogB)) || ($entity === null)) {
            return $entity;
        }

        throw new Exception('Invalid entity');
    }
}
Enter fullscreen mode Exit fullscreen mode

The above snippet in PHP8.2 with the introduction of DNF becomes:

class Blog {
    public function checkEntity((BlogA&BlogB)|null $entity) {
        return $entity;
    }
}
Enter fullscreen mode Exit fullscreen mode

How beautiful is this( specially if you are lazy like me :))

Stand-alone types( null, false, and true)

PHP 8.0 added support for Union Types, which made it possible to declare a type as a union of two or more types. If you are familiar with Typescript, this is the same concept. It allowed false and null as possible types for a Union Type, but disallowed their use as standalone types.

Typescript exampel:

function printId(id: number | string) {
  console.log("Your ID is: " + id);
}
Enter fullscreen mode Exit fullscreen mode

PHP8.0 union types example:

class Example {
    private int|float $foo;
    public function squareAndAdd(float|int $bar): int|float {
        return $bar ** 2 + $foo;
    }
}
Enter fullscreen mode Exit fullscreen mode

And now in PHP8.2, we can return either null, true, or false as in:


public function isAuthenticated(): true {
return true;
}

The above function is a function that returns true.
we can also use, null and false as return type.

Constants in Traits

Traits helps us to overcome the multiple inheritance, and now with PHP8.2, it is possible to define constants in traits.

trait NodeFunctions
{
    public const CONSTANT = 1;
}

class BlogType
{
    use NodeFunctions;
}

var_dump(NodeFunctions::CONSTANT); // Error
var_dump(BlogType::CONSTANT); // 1
Enter fullscreen mode Exit fullscreen mode

We can't access the constant through the name of the trait, but it is possible to it via the class that uses the trait.

Some of the new functions and attributes introduced are:

This is just a tip of an iceberg, more can be found at the official PHP8.2 release notes.

Thanks so for reading this far, and you are welcome to put your comments down and I would really appreciate if you give me a follow.

Top comments (0)

All DEV content is created by the community!

Hey, if you're landing here for the first time, you should know that this website is a global community of folks who blog about their experiences to help folks like you out.

Sign up now if you're curious. It's free!