Readonly properties let you create values that can only be set once and never changed. This helps keep your data safe and predictable.
Readonly properties in PHP
class ClassName { public readonly Type $propertyName; public function __construct(Type $value) { $this->propertyName = $value; } }
The readonly keyword is placed before the property type and name.
You can only assign a value to a readonly property once, usually in the constructor.
$id that can only be set when creating a User.<?php class User { public readonly int $id; public function __construct(int $id) { $this->id = $id; } }
$appName holds the application name and cannot be changed later.<?php class Config { public readonly string $appName; public function __construct(string $appName) { $this->appName = $appName; } }
This program creates a Product with readonly properties name and price. It prints the product details. Trying to change price after creation will cause an error.
<?php class Product { public readonly string $name; public readonly float $price; public function __construct(string $name, float $price) { $this->name = $name; $this->price = $price; } } $product = new Product("Book", 12.99); echo "Product: {$product->name}\n"; echo 'Price: $' . $product->price . "\n"; // Trying to change readonly property will cause error // $product->price = 15.99; // Uncommenting this line will cause an error
Readonly properties can only be assigned once, usually in the constructor.
Trying to change a readonly property after it is set will cause a fatal error.
Readonly properties help make your code safer and easier to understand.
Readonly properties store values that cannot be changed after being set.
They are useful for fixed data like IDs or configuration.
Assign them once, usually in the constructor, and then they stay constant.
Practice
readonly properties in PHP?Solution
Step 1: Understand readonly property behavior
Readonly properties in PHP are designed to hold values that can only be assigned once and cannot be modified later.Step 2: Compare options with this behavior
To store values that cannot be changed after being set correctly describes this behavior, while others describe unrelated features.Final Answer:
To store values that cannot be changed after being set -> Option AQuick Check:
Readonly means unchangeable after set = A [OK]
- Thinking readonly allows changes anytime
- Confusing readonly with private visibility
- Assuming readonly auto-generates methods
Solution
Step 1: Recall PHP property declaration order
In PHP, visibility (public/private/protected) comes first, then readonly keyword, then type and variable name.Step 2: Match syntax with options
public readonly int $id; matches the correct order:public readonly int $id;. Others have incorrect order.Final Answer:
public readonly int $id; -> Option BQuick Check:
Visibility then readonly then type = B [OK]
- Placing readonly before visibility
- Putting readonly after type
- Mixing order of keywords
class User {
public readonly string $name;
public function __construct(string $name) {
$this->name = $name;
}
}
$user = new User('Alice');
echo $user->name;
$user->name = 'Bob';
echo $user->name;Solution
Step 1: Understand readonly property assignment
The readonly property$nameis assigned once in the constructor to 'Alice'. This is allowed.Step 2: Analyze second assignment
Trying to assign$user->name = 'Bob';after construction causes a fatal error because readonly properties cannot be changed after initial assignment.Final Answer:
Alice followed by a fatal error -> Option AQuick Check:
Readonly blocks changes after set = C [OK]
- Assuming readonly properties can be changed anytime
- Expecting no error on second assignment
- Confusing syntax error with runtime error
class Config {
public readonly string $version;
public function __construct() {
$this->version = '1.0';
$this->version = '2.0';
}
}Solution
Step 1: Check property assignments in constructor
The readonly property$versionis assigned twice: first '1.0', then '2.0'.Step 2: Understand readonly assignment rules
Readonly properties can only be assigned once, even inside the constructor. Assigning twice causes an error.Final Answer:
Assigning readonly property twice in constructor -> Option DQuick Check:
Readonly assigned once only = A [OK]
- Assigning readonly property multiple times
- Ignoring visibility keyword importance
- Thinking readonly disallows string type
class User {
public readonly int $id;
public readonly string $name;
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
}Choose the correct usage:
Solution
Step 1: Check constructor usage
The constructor requires two parameters:int $idandstring $name. $user = new User(1, 'John'); // no changes after construction correctly calls the constructor with both.Step 2: Verify immutability
Readonly properties cannot be changed after construction. $user = new User(1, 'John'); // no changes after construction respects this by not modifying properties after creation.Final Answer:
$user = new User(1, 'John'); // no changes after construction -> Option CQuick Check:
Construct with all params, no changes after = D [OK]
- Trying to assign readonly properties after construction
- Calling constructor with missing arguments
- Assuming properties can be set without constructor
Ready to prove it?
Pass the test and this pattern moves unknown
