Recall & Review
beginner
What is a readonly property in PHP?
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?
✗ Incorrect
Readonly properties are declared using the
readonly keyword before the property name.When can you assign a value to a readonly property?
✗ 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?
✗ Incorrect
PHP throws an error because readonly properties cannot be modified after initial assignment.
Which PHP version introduced readonly properties?
✗ Incorrect
Readonly properties were introduced in PHP 8.1.
Why are readonly properties useful?
✗ 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.