0
0
PHPprogramming~20 mins

Readonly classes in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Readonly Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of readonly class property assignment
What will be the output of this PHP code using a readonly class?
PHP
<?php
readonly class User {
    public string $name;
    public function __construct(string $name) {
        $this->name = $name;
    }
}

$user = new User("Alice");
echo $user->name;
$user->name = "Bob";
echo $user->name;
AAliceBob
BFatal error: Cannot declare readonly class User
CAliceAlice
D
Alice
Fatal error: Cannot modify readonly property User::$name
Attempts:
2 left
💡 Hint
Readonly classes allow properties to be assigned only once, typically in the constructor.
🧠 Conceptual
intermediate
1:30remaining
Behavior of readonly classes with mutable properties
Which statement about readonly classes in PHP is true?
AReadonly classes prevent reassignment of properties but do not prevent mutation of objects assigned to properties.
BReadonly classes only allow properties to be assigned in the constructor and nowhere else, including nested objects.
CReadonly classes allow modification of properties if they are objects, but not scalar values.
DReadonly classes prevent modification of all properties, including objects referenced by properties.
Attempts:
2 left
💡 Hint
Think about how readonly affects references to objects stored in properties.
🔧 Debug
advanced
2:00remaining
Identify the error in readonly class property initialization
What error will this PHP code produce?
PHP
<?php
readonly class Product {
    public string $name = "Default";
    public function __construct() {
        $this->name = "Gadget";
    }
}

$product = new Product();
echo $product->name;
AGadget
BFatal error: Cannot initialize readonly property Product::$name twice
CDefault
DParse error: Unexpected token '='
Attempts:
2 left
💡 Hint
Readonly properties can only be assigned once, either at declaration or in constructor, not both.
📝 Syntax
advanced
1:30remaining
Valid syntax for declaring a readonly class
Which of the following PHP code snippets correctly declares a readonly class with a readonly property?
A
class Car readonly {
    public readonly string $model;
    public function __construct(string $model) {
        $this-&gt;model = $model;
    }
}
B
class readonly Car {
    public string $model readonly;
    public function __construct(string $model) {
        $this-&gt;model = $model;
    }
}
C
readonly class Car {
    public string $model;
    public function __construct(string $model) {
        $this-&gt;model = $model;
    }
}
D
readonly class Car {
    public readonly string $model;
    public function __construct(string $model) {
        $this-&gt;model = $model;
    }
}
Attempts:
2 left
💡 Hint
The readonly keyword must appear before class and property declarations correctly.
🚀 Application
expert
2:00remaining
Count properties in a readonly class after instantiation
Given this readonly class, how many properties does the object have after creation?
PHP
<?php
readonly class Book {
    public string $title;
    public string $author;
    public function __construct(string $title, string $author) {
        $this->title = $title;
        $this->author = $author;
    }
}

$book = new Book("1984", "Orwell");
$props = get_object_vars($book);
echo count($props);
A2
B1
C0
DFatal error: Cannot access properties of readonly class
Attempts:
2 left
💡 Hint
Readonly properties are still accessible and countable like normal properties.