Tsallaka zuwa abun ciki
Justice Tech School

readonly Properties

readonly properties can be set once, then never changed. Great for value objects.

Example
class P { function __construct(public readonly int $x) {} }
$p = new P(5);
try { $p->x = 6; } catch (Error $e) { echo "read only"; }

Result

read only

Exercise

Set once.

public  int $x

Quiz Test

When can a readonly prop be set?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.