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!