Author: Manuel Lemos
Viewers: 253
Last month viewers: 1
Categories: PHP Tutorials, News
Read this short article to learn more about reading-only classes' benefits.
What Are Readonly PHP Classes?
PHP 8.1 introduced read-only properties. In this context, you can understand that properties can also be called class variables.
Read-only class variables cannot be changed after an object of a class is initialized.
In PHP 8.2 read-only classes turn all class properties automatically as read-only.
The PHP manual gives good examples of what are read-only classes, read-only class properties, and how you can emulate them before PHP 8.1:
In PHP 8.2:
readonly class BlogData { public string $title; public Status $status; public function __construct(string $title, Status $status) { $this->title = $title; $this->status = $status; } }
In PHP 8.1:
class BlogData { public readonly string $title; public readonly Status $status; public function __construct(string $title, Status $status) { $this->title = $title; $this->status = $status; } }
Before PHP 8.1:
class BlogData { private Status $status; private string $title; public function __construct(string $title, Status $status) { $this->status = $status; } public function getStatus(): Status { return $this->status; } public function getTitle(): string { return $this->title; } }
Why Read-Only Properties and Classes Are a Good Thing For PHP Developers
Readonly classes can be more useful when you have many class properties that you do not want to change after a class object is initialized.
As you see in the example code above, you can verify that before PHP 8.1, read-only classes could be emulated using private class properties and getter functions to access those properties. That would work, but you would need to write a lot more code.
So with PHP 8.2, you can have read-only classes writing less code.
How You Can Use Read-Only Classes in Practice?
Read-only classes can be used, for instance, to set up configuration objects that have properties that contain application configuration values.
Those configuration values can be initialized once. They should not be changed later because the values should remain the same in all parts of your application that need to access those configuration values.
This is just one example of a situation in which read-only class properties can be useful.
readonly class ApplicationConfiguration { public string $application_title; public string $database_connection; public function __construct(string $application_title, $database_connection) { $this->application_title = $application_title; $this->database_connection = $database_connection; } } $configuration = new ApplicationConfiguration('My Application', 'mysql:host=localhost;port=3307;dbname=testdb'); echo '<h1>Welcome to the ', $configuration->application_title, '</h1>';
You need to be a registered user or login to post a comment
1,616,820 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
No comments were submitted yet.