[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

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 the read-only class added in PHP 8.2.

So let's take a look.

readonly class

PHP 8.1 now allows you to set the readonly modifier on properties.

PHP 8.2 now allows you to set the readonly modifier on classes.

<?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

You can see that when you try to rewrite the property in the readonly class, the following error occurs.

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

Additionally, creating dynamic properties is also prohibited.

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

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

Additionally, dynamic properties have been deprecated since PHP 8.2.

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

However, using #[AllowDynamicProperties] in a readonly class is prohibited, and attempting to use #[AllowDynamicProperties] in a readonly class will result in an error like the one below.

<?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

Please note that an error will occur even if the type is not defined for the property.

inheritance

Although it is possible to inherit a readonly class from a readonly class,

It is prohibited to inherit a readonly class from a non-readonly class, or from a non-readonly class to 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

By using the method below, you can now check whether the class is readonly or not.

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

Differences from final class modifier

We'll also touch on the difference between read only classes and final class modifiers.

As explained above, it was possible for read only classes to inherit from each other, but classes with the final class modifier

Unable to extend class.

By the way, if you do not want to extend the class or rewrite the value, 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

By using the readonly class, you can save time by not having to set the readonly modifier for each property.

Once a value is set, it cannot be changed, and dynamic properties can 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 I got the impression that there were a lot of small additions.

Personally, I think the deprecation of dynamic properties will be a big deal in the future of PHP.

(If you declare a class property outside the class, you will not know where the property is declared.

This is because I believe that debugging efficiency will decrease. )

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

I would also like to introduce you!

Have a nice PHP life!

If you found this article helpful , please give it a like!
7
Loading...
7 votes, average: 1.00 / 17
1,525
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

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.