I tried out ReadOnly Class added in PHP 8.2!

Hello! This is Koide from the System Development Department!

PHP 8.2 was released on December 8th!

Today I will write about read-only classes, which were added in PHP 8.2

Let's take a look right away

Read-only class

In PHP 8.1, it is now possible to set the readonly modifier on a property,

In PHP 8.2 it is now possible to set the readonly modifier on a class

<?php readonly class SekarakuBlog { public string $title; }

The above will make all properties in the class readonly

<?php readonly class SekarakuBlog { public string $title; public function __construct() { $this-> title = 'test Title'; } } $test = new SekarakuBlog(); $test->title = 'Change Title'; // Fatal error: Uncaught Error: Cannot modify readonly property SekarakuBlog::$title $test->content = 'content'; // Fatal error: Uncaught Error: Cannot create dynamic property SekarakuBlog::$content

If you try to rewrite a property in a readonly class, you will see the following error:

Fatal error: Uncaught Error: Cannot modify readonly property SekarakuBlog::$title

Additionally, the creation of dynamic properties is prohibited

When I try to create a dynamic property I get the following error:

Fatal error: Uncaught Error: Cannot create dynamic property SekarakuBlog::$content

Also, dynamic properties have been deprecated since PHP 8.2

You can still use dynamic properties by including the #[AllowDynamicProperties] attribute

However, using #[AllowDynamicProperties] in a readonly class is prohibited, and if you try to use #[AllowDynamicProperties] in a readonly class, you will get the following error:

<?php #[AllowDynamicProperties] readonly class SekarakuBlog {} // Fatal error: Cannot apply #[AllowDynamicProperties] to readonly class SekarakuBlog

mold

<?php readonly class SekarakuBlog { public $title; public function __construct() { $this->title = 'test Title'; } } $test = new SekarakuBlog(); // Fatal error: Readonly property SekarakuBlog::$title must have type

Note that an error will occur if you do not define a type for the property

Inheritance

It is possible to inherit a readonly class from another readonly class,

It is prohibited to inherit a readonly class with a non-readonly class, or to inherit a non-readonly class with a readonly class

# The following is OK readonly class SekarakuBlog {} readonly class ExtendSekaraku extends SekarakuBlog {} # The following is NG class SekarakuBlog {} readonly class ExtendSekaraku extends SekarakuBlog {} // Fatal error: Readonly class ExtendSekaraku cannot extend non-readonly class SekarakuBlog readonly class SekarakuBlog {} class ExtendSekaraku extends SekarakuBlog {} // Fatal error: Non-readonly class ExtendSekaraku cannot extend readonly class SekarakuBlog

You can now check whether a class is readonly or not by using the following method

readonly class SekarakuBlog {} $reflectionClass = new ReflectionClass('SekarakuBlog'); $reflection->isReadOnly(); // true

Difference from the final class modifier

We will also briefly touch on the difference between read only classes and the final class modifier

As explained above, read-only classes can inherit from each other, but classes with the final class modifier cannot

You cannot extend a class

By the way, if you don't want to extend the class or rewrite the values, you can write the following

<?php final readonly class Sekaraku { public string $hoge; public function __construct() { $this-> hoge = 'hoge'; } } $test = new Sekaraku(); $test->hoge = 'test'; // Fatal error: Uncaught Error: Cannot modify readonly property Sekaraku::$hoge in /var/www/html/docker/php/finalreadonly.php:17 readonly class Lab extends Sekaraku {} $test2 = new Lab(); // Fatal error: Class Lab cannot extend final class Sekaraku in /var/www/html/docker/php/finalreadonly.php on line 20

merit

Using the readonly class saves you the trouble of having to set the readonly modifier for each property

Once a value is set, it cannot be rewritten, and dynamic properties can also be prohibited, so it seems possible to write more robust code

summary

There were other features added in PHP 8.2 this time, but overall it seemed like there were a lot of small additions

Personally, I think the deprecation of dynamic properties is a big deal for the future of PHP

(When you declare a class property outside the class, it becomes difficult to know where the property is declared

This is because I think it will reduce debugging efficiency.)

I think there are many people using it, so you need to be careful when upgrading the version

I hope to introduce it to you again!

Have a good PHP life!

If you found this article helpful , please give it a like!
7
Loading...
7 votes, average: 1.00 / 17
2,357
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Shōgo Koide

Born in Osaka Prefecture. He is an engineer who loves saunas. On his days off, he goes to the sauna and communicates with the universe. She holds an in-house meeting called Asamokukai every weekday morning from 8:30 to 9:30, and is the chairperson of the Asamokukai. In his work, he is mainly responsible for development in PHP.