Readonly classes in PHP - Time & Space Complexity
Let's see how using readonly classes affects the time it takes for a program to run.
We want to know how the program's steps grow when working with readonly classes.
Analyze the time complexity of the following code snippet.
final readonly class UserData {
public function __construct(
public string $name,
public int $age
) {}
}
$users = [];
for ($i = 0; $i < $n; $i++) {
$users[] = new UserData("User$i", $i);
}
This code creates many readonly UserData objects and stores them in an array.
Look for loops or repeated actions.
- Primary operation: Creating a new UserData object inside a loop.
- How many times: The loop runs
ntimes, sonobjects are created.
Each new object takes a small, fixed amount of time to create.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 object creations |
| 100 | 100 object creations |
| 1000 | 1000 object creations |
As n grows, the total work grows in a straight line with it.
Time Complexity: O(n)
This means the time to create all readonly objects grows directly with the number of objects.
[X] Wrong: "Readonly classes make object creation instant or free."
[OK] Correct: Readonly means properties can't change after creation, but creating each object still takes time proportional to how many you make.
Understanding how object creation scales helps you write clear and efficient code, a skill valued in many coding discussions.
"What if we added a nested loop inside the constructor that runs m times? How would the time complexity change?"
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
