PHP Classes

How Can You Benefit from PHP Readonly Classes Improvements Since PHP 8.2 with PHP Code Examples

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How Can You Benefit f...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Updated on: 2023-01-31

Posted on: 2023-01-31

Viewers: 93 (February 2023 until August 2023)

Last month viewers: 1 (August 2023)

Categories: PHP Tutorials, News

PHP readonly classes is the name of the new feature introduced in PHP 8.2 that was released on December 2022.

The readonly classes are an evolution of the readonly class properties introduced in PHP 8.1.

Read this short tutorial article to learn more about readonly classes and how you can benefit from this feature if you use PHP 8.2 or later by checking some PHP code examples.




Loaded Article

In this article you will learn about:

1. What Are Readonly PHP Classes

2. Why Would You Want to Use PHP Readonly Classes

3. How You can Benefit of The Improvements to Readonly Classes in PHP 8.2


1. What Are Readonly PHP Classes

PHP readonly classes is the name of a feature improvement done for PHP 8.2.

According to the Request For Change document (RFC) about PHP readonly classes, this improvement is an evolution of PHP readonly properties introduced in PHP 8.1 to simplify the declaration of classes that only have readonly properties.

Before PHP 8.2, you would need to write classes with readonly properties like this:

class user
{
    readonly public $name;

    readonly public $email;
};

Since PHP 8.2, you can declare readonly classes like this:

readonly class user
{
    public $name;

    public $email;
};

2. Why Would You Want to Use PHP Readonly Classes

PHP is a dynamic programming language. As a developer, you can change the value and type of a variable anywhere in your PHP scripts.

The PHP code execution engine provides this flexibility at the expense of duplicating the use of memory every time you change a variable to a new dynamically created value because the PHP engine needs to allocate new memory space for the new value of a variable.

This process also takes more CPU because every time a new variable value is assigned, the CPU must be used to copy the new variable value to the memory space that is used to store the new variable value.

When you have a variable that you assign to an object of a readonly class, the values of the class object cannot be changed after the object creation. Therefore there will be no new memory allocation when the class object is assigned to a new variable or passed to a function as a parameter.

It also takes much less CPU time to assign a variable or pass as a function parameter the value of a readonly class object.

Your application's code cannot also change readonly class objects after the object is created. This also makes your code more secure against possible bugs in code that may try to change the readonly class object.

3. How You can Benefit of The Improvements to Readonly Classes in PHP 8.2

Suppose you have a class in your application that stores application configuration values that you can declare like this:

<?php

readonly class configuration
{
    public string $applicationName = 'My application';

    public function __construct($initialConfiguration) {
        $this->applicationName = $initialConfiguration['applicationName'];
    }
};

You can have a local_configuration.php script file like this:

<?php

return [
    'applicationName'=>'My development application'
];

You can load an array with custom values to change the configuration values to other matters specific to your environment.

This is useful for customizing application configuration values depending on whether you are in the development, testing, or production environments.

$localConfiguration = require 'local_configuration.php';

Then you can create an object of the configuration class using the $localConfiguration array to change the $configuration object in the class constructor function.

$configuration = new configuration($localConfiguration);

echo $configuration->applicationName;

However, changing the configuration object is not allowed, and it will give an error. This way, you can find mistaken attempts to change the readonly class variables after the class object is created.

$configurationName->applicationName = 'My local application';

The line of code above will give an error like below when you execute this example code in PHP 8.2:

Fatal error: Readonly property configuration::$applicationName cannot have default value in test_readonly_class.php on line 5



You need to be a registered user or login to post a comment

1,611,062 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

No comments were submitted yet.



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How Can You Benefit f...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)