Back to questions
JuniorTheoryCommon
What does the program print?
<?php
enum Status: string
{
case Draft = 'draft';
case Live = 'live';
}
$s = Status::from('live');
$t = Status::tryFrom('x');
echo $s->name, ' ', $s->value, ' ', $t === null ? 'none' : 'some', "\n";Live live none
- Expecting
nameto be the backing value:nameis the case's identifier as written (Live), whilevalueis the scalar behind it ('live'). - Expecting
tryFromto throw likefrom:fromthrows aValueErrorfor an unknown value, whiletryFromreturnsnull, which you check or replace with??.
- When would you choose
fromovertryFromfor a value that came from a request? - What does
Status::cases()return?