0
0
PHPprogramming~10 mins

Readonly classes in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Readonly classes
Define readonly class
Create instance with properties
Try to modify property?
YesError: Cannot modify readonly property
No
Use properties as read-only
End
This flow shows how a readonly class is defined, instantiated, and how its properties cannot be changed after creation.
Execution Sample
PHP
<?php
readonly class User {
  public string $name;
  public function __construct(string $name) {
    $this->name = $name;
  }
}
$user = new User("Alice");
$user->name = "Bob"; // Error
?>
Defines a readonly class User with a name property, creates an instance, then tries to change the name property which causes an error.
Execution Table
StepActionProperty 'name' ValueResult/Output
1Define readonly class UserN/AClass User is readonly
2Create User instance with name='Alice'name = 'Alice'Instance created successfully
3Attempt to assign name='Bob'name = 'Alice'Error: Cannot modify readonly property User::$name
4Endname = 'Alice'Execution stops due to error
💡 Execution stops at step 3 because readonly property cannot be modified after initialization
Variable Tracker
VariableStartAfter Step 2After Step 3Final
$user->nameundefinedAliceAliceAlice
Key Moments - 2 Insights
Why can't we change the property 'name' after creating the User object?
Because the class is declared as readonly, its properties can only be set once during construction. Step 3 in the execution_table shows the error when trying to modify 'name'.
Is it allowed to set the property 'name' inside the constructor?
Yes, setting properties inside the constructor is allowed and is how readonly properties get their initial value, as shown in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $user->name after step 3?
A"Alice"
B"Bob"
Cundefined
Dnull
💡 Hint
Check the 'Property 'name' Value' column at step 3 in the execution_table.
At which step does the program stop due to an error?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Result/Output' column for the error message in the execution_table.
If the class was not declared readonly, what would happen at step 3?
AError would still occur
BThe program would crash
CThe property 'name' would change to 'Bob'
DThe property would become null
💡 Hint
Consider how normal class properties behave when assigned after construction.
Concept Snapshot
readonly class ClassName {
  public type $property;
  public function __construct(type $property) {
    $this->property = $property; // Allowed
  }
}
Properties cannot be changed after construction; attempts cause errors.
Full Transcript
This example shows how to define a readonly class in PHP. The class User has a readonly property 'name' set in the constructor. When we create a User object with name 'Alice', it works fine. But if we try to change 'name' later to 'Bob', PHP throws an error because readonly properties cannot be modified after initialization. The execution table traces these steps clearly, showing the error at step 3. The variable tracker confirms the property value stays 'Alice'. This helps beginners understand readonly classes by watching the code run step-by-step.