Complete the code to declare a readonly property in a PHP class.
<?php class User { public [1] string $name; }
const instead of readonly for properties.static which is unrelated to immutability.The readonly keyword declares a property that can only be assigned once, typically in the constructor.
Complete the constructor to assign a value to the readonly property.
<?php class User { public readonly string $name; public function __construct([1]) { $this->name = $name; } }
int or bool which causes type errors.The constructor parameter must match the property type string to assign it correctly.
Fix the error by completing the code to prevent modifying a readonly property after construction.
<?php class User { public readonly string $name; public function __construct(string $name) { $this->name = $name; } public function changeName([1]) { $this->name = $newName; } }
The method tries to assign a new value to a readonly property, which is not allowed. The parameter type should be string, but the assignment itself will cause an error.
Fill both blanks to create a readonly property and assign it in the constructor.
<?php class Product { public [1] int $id; public function __construct([2]) { $this->id = $id; } }
string type for the constructor parameter when property is int.readonly keyword.The property is declared as readonly and of type int. The constructor parameter must match the type int $id to assign it properly.
Fill all three blanks to declare a readonly property, assign it in the constructor, and prevent modification.
<?php class Order { public [1] float $amount; public function __construct([2]) { $this->amount = $amount; } public function updateAmount([3]) { $this->amount = $newAmount; } }
The property is declared readonly and of type float. The constructor parameter matches the property type. The update method tries to assign a new value, but this will cause an error because the property is readonly.