A readonly property is a class property that can only be assigned once, typically during object construction, and cannot be changed afterward.
Click to reveal answer
beginner
How do you declare a readonly property in PHP?
You declare a readonly property by adding the readonly keyword before the property type and name inside a class.
Click to reveal answer
beginner
Can you assign a value to a readonly property outside the constructor in PHP?
No, once a readonly property is assigned a value (usually in the constructor), it cannot be changed later anywhere else in the code.
Click to reveal answer
intermediate
What happens if you try to modify a readonly property after it has been set?
PHP will throw an error because readonly properties are immutable after their initial assignment.
Click to reveal answer
beginner
Why use readonly properties in PHP classes?
Readonly properties help protect data by preventing accidental changes, making objects more predictable and safer to use.
Click to reveal answer
How do you declare a readonly property in PHP?
AUse the keyword <code>readonly</code> before the property name
BUse the keyword <code>const</code> before the property name
CUse the keyword <code>final</code> before the property name
DUse the keyword <code>static</code> before the property name
✗ Incorrect
Readonly properties are declared using the readonly keyword before the property name.
When can you assign a value to a readonly property?
AOnly after the object is destroyed
BMultiple times anywhere in the class
COnly once, typically in the constructor
DOnly inside static methods
✗ Incorrect
Readonly properties can be assigned only once, usually during object construction.
What happens if you try to change a readonly property after it is set?
APHP throws an error
BThe property value changes silently
CThe property resets to null
DThe program ignores the change
✗ Incorrect
PHP throws an error because readonly properties cannot be modified after initial assignment.
Which PHP version introduced readonly properties?
APHP 8.0
BPHP 8.1
CPHP 7.4
DPHP 7.0
✗ Incorrect
Readonly properties were introduced in PHP 8.1.
Why are readonly properties useful?
AThey make properties private
BThey make properties static
CThey allow properties to be changed anytime
DThey prevent accidental changes to important data
✗ Incorrect
Readonly properties help keep data safe by preventing accidental changes.
Explain what readonly properties are and how they work in PHP.
Think about properties that you set once and then keep safe.
You got /4 concepts.
Describe a situation where using readonly properties would be helpful in a PHP program.
Consider data that should never change after creation.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of readonly properties in PHP?
easy
A. To store values that cannot be changed after being set
B. To allow properties to be changed anytime
C. To make properties private
D. To automatically generate getter methods
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 A
Quick Check:
Readonly means unchangeable after set = A [OK]
Hint: Readonly means value set once and then fixed [OK]
Common Mistakes:
Thinking readonly allows changes anytime
Confusing readonly with private visibility
Assuming readonly auto-generates methods
2. Which of the following is the correct syntax to declare a readonly property in a PHP class?
easy
A. readonly public int $id;
B. public readonly int $id;
C. public int readonly $id;
D. int public readonly $id;
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 B
Quick Check:
Visibility then readonly then type = B [OK]
Hint: Visibility keyword comes before readonly in declaration [OK]
Common Mistakes:
Placing readonly before visibility
Putting readonly after type
Mixing order of keywords
3. What will be the output of the following PHP code?
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;
medium
A. Alice followed by a fatal error
B. BobBob
C. AliceBob
D. Syntax error
Solution
Step 1: Understand readonly property assignment
The readonly property $name is 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 A
Quick Check:
Readonly blocks changes after set = C [OK]
Hint: Readonly allows one assignment only, later changes cause error [OK]
Common Mistakes:
Assuming readonly properties can be changed anytime
Expecting no error on second assignment
Confusing syntax error with runtime error
4. Identify the error in the following PHP code using readonly properties:
class Config {
public readonly string $version;
public function __construct() {
$this->version = '1.0';
$this->version = '2.0';
}
}
medium
A. No error, code is correct
B. Missing visibility keyword for property
C. Readonly keyword used incorrectly with string type
D. Assigning readonly property twice in constructor
Solution
Step 1: Check property assignments in constructor
The readonly property $version is 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 D
Quick Check:
Readonly assigned once only = A [OK]
Hint: Assign readonly property only once, even in constructor [OK]
Common Mistakes:
Assigning readonly property multiple times
Ignoring visibility keyword importance
Thinking readonly disallows string type
5. Given this PHP class with readonly properties, which code correctly creates an immutable user with an ID and name?
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:
hard
A. $user = new User(1, 'John'); $user->name = 'Jane';
B. $user = new User(1); $user->name = 'John';
C. $user = new User(1, 'John'); // no changes after construction
D. $user = new User(); $user->id = 1; $user->name = 'John';
Solution
Step 1: Check constructor usage
The constructor requires two parameters: int $id and string $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 C
Quick Check:
Construct with all params, no changes after = D [OK]
Hint: Call constructor fully, then do not modify readonly properties [OK]
Common Mistakes:
Trying to assign readonly properties after construction
Calling constructor with missing arguments
Assuming properties can be set without constructor
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.