30
loading...
This website collects cookies to deliver better user experience
enum Season{
case Spring;
case Summer;
case Autumn;
case Winter;
}
function(Season $season){
if($season === Season::Summer){
echo 'It summer! It must be hot!';
}
}
enum DayOfWeek: int {
case Monday = 1;
case Tuesday = 2;
case Wednesday = 3;
case Thursday = 4;
case Friday = 5;
case Saturday = 6;
case Sunday = 7
}
DayOfWeek::Friday->value // It will return 5
::from()
and ::tryFrom()
.DayOfWeek::from(5)
DayOfWeek Enum:int
(
[name] => Thursday
[value] => 4
)
enum MatchStatus {
case NotStarted;
case FirstHalf;
case SecondHalf;
case FinishedRegularTime;
case FinishedAfterPenalties;
public function type(): string {
return match($this) {
Status::NotStarted => 'Not started',
Status::FirstHalf, Status::SecondHalf => 'Ongoing',
Status::FinishedRegularTime, Status::FinishedAfterPenalties => 'Finished',
};
}
}
type()
method, which will return status type as string value. This is how we can use it:$matchStatus = MatchStatus::SecondHalf;
$matchStatus->type();
'Ongoing'
string. If you haven't used PHP8 yet, You can be not familiar with match
expression. Read about it here.