Challenge - 5 Problems
Readonly Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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;
Attempts:
2 left
💡 Hint
Readonly classes allow properties to be assigned only once, typically in the constructor.
✗ Incorrect
The readonly class User allows property $name to be set only once during construction. The first echo prints 'Alice'. The second assignment to $user->name causes a fatal error because the property is readonly.
🧠 Conceptual
intermediate1:30remaining
Behavior of readonly classes with mutable properties
Which statement about readonly classes in PHP is true?
Attempts:
2 left
💡 Hint
Think about how readonly affects references to objects stored in properties.
✗ Incorrect
Readonly classes prevent reassigning properties after initialization, but if a property holds an object, the object's internal state can still be changed because the reference itself is readonly, not the object.
🔧 Debug
advanced2: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;
Attempts:
2 left
💡 Hint
Readonly properties can only be assigned once, either at declaration or in constructor, not both.
✗ Incorrect
Assigning a default value and then reassigning in the constructor causes a fatal error because the readonly property is initialized twice.
📝 Syntax
advanced1:30remaining
Valid syntax for declaring a readonly class
Which of the following PHP code snippets correctly declares a readonly class with a readonly property?
Attempts:
2 left
💡 Hint
The readonly keyword must appear before class and property declarations correctly.
✗ Incorrect
Option C correctly uses 'readonly class Car' where the property is implicitly readonly. Option C causes a compile-time error because properties cannot be declared explicitly readonly in readonly classes. Options A and B have invalid syntax.
🚀 Application
expert2: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);
Attempts:
2 left
💡 Hint
Readonly properties are still accessible and countable like normal properties.
✗ Incorrect
Readonly properties are normal properties that cannot be reassigned after initialization. get_object_vars returns all accessible properties, so count is 2.