Readonly classes help keep data safe by making sure once you set values, they cannot be changed later.
Readonly classes in PHP
readonly class ClassName { public function __construct( public Type $property1, public Type $property2 ) {} }
The readonly keyword before class means all properties are readonly.
You must set all properties when creating the object, usually in the constructor.
User with two properties. Once set, they cannot be changed.<?php readonly class User { public function __construct( public string $name, public int $age ) {} } $user = new User("Alice", 30); echo $user->name . " is " . $user->age . " years old.";
Point for fixed coordinates.<?php readonly class Point { public function __construct( public float $x, public float $y ) {} } $point = new Point(1.5, 2.5); echo "Point coordinates: (" . $point->x . ", " . $point->y . ")";
This program creates a readonly class Book with title and author. It prints the book details. Trying to change the title after creation will cause an error.
<?php readonly class Book { public function __construct( public string $title, public string $author ) {} } $book = new Book("1984", "George Orwell"); echo "Book: " . $book->title . " by " . $book->author . "\n"; // Trying to change a property will cause an error // $book->title = "Animal Farm"; // This will fail
Readonly classes were introduced in PHP 8.2.
Once an object of a readonly class is created, you cannot change its properties.
This helps prevent bugs caused by unexpected data changes.
Readonly classes make all properties unchangeable after creation.
They are useful for fixed data that should not be modified.
Trying to change a readonly property will cause an error.
Practice
readonly class in PHP ensure about its properties?Solution
Step 1: Understand the meaning of readonly class
A readonly class means all its properties are readonly by default, so they cannot be changed after the object is created.Step 2: Compare options with this definition
Only the option stating "All properties cannot be changed after the object is created." correctly states that properties cannot be changed after creation.Final Answer:
All properties cannot be changed after the object is created. -> Option CQuick Check:
Readonly class = immutable properties [OK]
- Thinking readonly means private or static
- Assuming properties can be changed anytime
- Confusing readonly with access modifiers
Solution
Step 1: Recall PHP readonly class syntax
In PHP, the keywordreadonlycomes beforeclassto declare a readonly class.Step 2: Check each option
readonly class User {} correctly usesreadonly class User {}. class readonly User {} reverses the order, class User readonly {} places readonly incorrectly, and readonly class User { public readonly string $name; } is invalid syntax.Final Answer:
readonly class User {} -> Option DQuick Check:
Keyword readonly before class [OK]
- Placing readonly after class name
- Using readonly as a property only
- Mixing readonly with access modifiers
readonly class Point {
public int $x;
public int $y;
public function __construct(int $x, int $y) {
$this->x = $x;
$this->y = $y;
}
}
$p = new Point(5, 10);
echo $p->x . ', ' . $p->y;
Solution
Step 1: Understand readonly class property initialization
Readonly properties can be set inside the constructor. Here,$xand$yare set to 5 and 10 respectively.Step 2: Check output of echo statement
The echo prints5, 10as expected because the properties were initialized correctly and not modified later.Final Answer:
5, 10 -> Option AQuick Check:
Readonly properties set in constructor output values [OK]
- Thinking readonly properties cannot be set at all
- Expecting error on property access
- Confusing output format with space instead of comma
readonly class User {
public string $name;
public function __construct(string $name) {
$this->name = $name;
}
}
$user = new User('Alice');
$user->name = 'Bob';Solution
Step 1: Understand readonly property behavior
In a readonly class, properties can only be assigned once during construction. Changing them later causes an error.Step 2: Analyze the code
The code tries to change$user->nameafter construction, which is not allowed and causes an error.Final Answer:
Cannot assign to readonly property after construction -> Option AQuick Check:
Readonly properties are immutable after construction [OK]
- Thinking readonly properties can be changed anytime
- Confusing readonly class with readonly property keyword
- Ignoring error on property reassignment
readonly class User {
public string $name;
public function __construct(string $name) {
$this->name = $name;
}
}Choose the correct approach:
Solution
Step 1: Understand immutability of readonly classes
Readonly class properties cannot be changed after construction, so to 'change' data, create a new object with new values.Step 2: Evaluate each option
$user = new User('Alice'); $user->name = 'Bob'; tries to change property directly - error. $user = new User('Alice'); $user->name = strtoupper($user->name); tries to modify property - error. $user = new User('Alice'); $newUser = new User('Bob'); creates a new User object with new name - correct. $user = new User('Alice'); unset($user->name); tries to unset property - invalid.Final Answer:
Create a new User object with updated name -> Option BQuick Check:
Immutable means create new object for changes [OK]
- Trying to modify readonly property directly
- Using unset on readonly properties
- Assuming readonly means properties can be changed
Ready to prove it?
Pass the test and this pattern moves unknown
