Skip to content
Omni Academy
Search courses and lessons
Search
Courses
Backend Development
9
Databases
1
Frontend Development
5
Data Science
5
All courses →
Playground
HTML & CSS
JavaScript
TypeScript
Python
PHP
Ruby
Light
Dark
Auto
Sign in
Get started
All
Backend Development
Databases
Frontend Development
Data Science
Playground
Blog
HTML
JS
TS
PY
PHP
RB
← Courses
📄
Classes and enums
Run as I type
Reset
▶
Run
Ctrl
↵
PHP
PHP source code
<?php enum Level: string { case Basic = "basic"; case Expert = "expert"; public function label(): string { return ucfirst($this->value); } } final class Learner { // Constructor promotion: declared and assigned in one place. public function __construct( public readonly string $name, public readonly int $score, public readonly Level $level = Level::Basic, ) {} public function passed(int $mark = 80): bool { return $this->score >= $mark; } public function __toString(): string { return sprintf("%s (%d, %s)", $this->name, $this->score, $this->level->label()); } } $learners = [ new Learner("Aisha", 92, Level::Expert), new Learner("Kenji", 78), ]; foreach ($learners as $learner) { echo $learner, $learner->passed() ? " PASS\n" : " ...\n"; } // readonly means this is an error rather than a silent change: // $learners[0]->score = 100;
Input
one line per
input()
call
Output
Rendered by your browser