0
0
PHPprogramming~10 mins

Readonly properties in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Readonly properties
Create object
Assign readonly property once
Try to assign again?
YesError: Cannot modify readonly property
No
Use property value
End
Readonly properties in PHP allow setting a property only once during object creation or initialization. Any further changes cause an error.
Execution Sample
PHP
<?php
class User {
  public readonly string $name;
}
$user = new User();
$user->name = "Alice";
$user->name = "Bob"; // Error
?>
This code creates a User object with a readonly property 'name' set once after creation. Trying to change it later causes an error.
Execution Table
StepActionProperty 'name' ValueResult
1Create User objectUnsetObject created
2Assign 'Alice' to readonly property 'name'AliceAssignment successful
3Try to assign 'Bob' to readonly property 'name'AliceError: Cannot modify readonly property
4Use property 'name' valueAliceOutputs 'Alice'
💡 Execution stops at step 3 due to error when trying to modify readonly property
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$user->nameUnsetUnsetAliceAlice (unchanged due to error)Alice
Key Moments - 2 Insights
Why can't we change the value of a readonly property after it's set?
Readonly properties in PHP are designed to be assigned only once. As shown in execution_table step 3, trying to assign a new value causes an error, preventing modification.
When can we assign a value to a readonly property?
You can assign a value only once, typically during object creation or inside the constructor, as shown in step 2 where the property is set successfully.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $user->name after step 2?
A"Bob"
B"Alice"
CUnset
DError
💡 Hint
Check the 'Property 'name' Value' column at step 2 in the execution_table.
At which step does the error occur when trying to modify the readonly property?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Result' column in execution_table for the error message.
If we remove the second assignment ($user->name = "Bob";), how does the execution table change?
AStep 3 is removed, no error occurs
BStep 2 causes an error
CStep 4 outputs 'Bob'
DStep 1 fails to create object
💡 Hint
Refer to execution_table rows and see what causes the error.
Concept Snapshot
Readonly properties in PHP:
- Declared with 'public readonly'
- Can be assigned only once (usually in constructor)
- Further assignments cause error
- Useful for immutable object data
- Helps prevent accidental changes
Full Transcript
This visual execution trace shows how readonly properties in PHP work. First, an object is created with a readonly property 'name'. The property is assigned once successfully after creation. When the code tries to assign a new value to the readonly property, PHP throws an error and stops execution. The variable tracker shows the property value remains unchanged after the error. Key moments clarify that readonly properties can only be set once and cannot be changed later. The quiz questions help reinforce understanding by asking about property values at different steps and the error occurrence.