0
0
PHPprogramming~20 mins

Readonly properties in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Readonly Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of readonly property assignment

What will be the output of this PHP code using readonly properties?

PHP
<?php
class User {
    public readonly string $name;
    public function __construct(string $name) {
        $this->name = $name;
    }
}
$user = new User("Alice");
echo $user->name;
?>
AAlice
BFatal error: Cannot modify readonly property User::$name
Cnull
DParse error: syntax error, unexpected 'readonly'
Attempts:
2 left
💡 Hint

Readonly properties can be assigned once, usually in the constructor.

Predict Output
intermediate
2:00remaining
Error on modifying readonly property after construction

What happens when you try to modify a readonly property after the object is created?

PHP
<?php
class Point {
    public readonly int $x;
    public function __construct(int $x) {
        $this->x = $x;
    }
}
$p = new Point(5);
$p->x = 10;
echo $p->x;
?>
A10
BFatal error: Cannot modify readonly property Point::$x
C5
DParse error: syntax error
Attempts:
2 left
💡 Hint

Readonly properties cannot be changed after initial assignment.

🧠 Conceptual
advanced
2:00remaining
Readonly property initialization timing

When can a readonly property be assigned a value in PHP?

AOnly once, but anywhere in the class methods
BMultiple times anywhere in the class methods
COnly once, and only inside the constructor or at property declaration
DOnly at the moment of object destruction
Attempts:
2 left
💡 Hint

Think about when readonly properties get their value safely.

🔧 Debug
advanced
2:00remaining
Identify the error with readonly property usage

What error will this code produce?

PHP
<?php
class Config {
    public readonly array $settings = [];
    public function addSetting(string $key, string $value): void {
        $this->settings[$key] = $value;
    }
}
$config = new Config();
$config->addSetting('theme', 'dark');
print_r($config->settings);
?>
AEmpty array output
BArray ( [theme] => dark )
CParse error: syntax error, unexpected '['
DFatal error: Cannot modify readonly property Config::$settings
Attempts:
2 left
💡 Hint

Readonly means the property cannot be changed after initialization, including its contents.

🚀 Application
expert
2:00remaining
Using readonly properties with nested objects

Consider this code. What will be the output?

PHP
<?php
class Address {
    public readonly string $city;
    public function __construct(string $city) {
        $this->city = $city;
    }
}
class Person {
    public readonly Address $address;
    public function __construct(Address $address) {
        $this->address = $address;
    }
}
$addr = new Address('Paris');
$person = new Person($addr);
$person->address->city = 'London';
echo $person->address->city;
?>
AFatal error: Cannot modify readonly property Address::$city
BParis
CLondon
DFatal error: Cannot modify readonly property Person::$address
Attempts:
2 left
💡 Hint

Readonly applies to the property itself, but what about the object it points to?