Bird
0
0

Consider this PHP code:

hard📝 Application Q9 of 15
PHP - Classes and Objects
Consider this PHP code:
class Status {
    const OK = 1;
    const ERROR = 0;
    public static function getMessage($code) {
        return match($code) {
            self::OK => 'Success',
            self::ERROR => 'Failure',
            default => 'Unknown'
        };
    }
}
echo Status::getMessage(Status::OK);

What is the output?
AOK
BError
C1
DSuccess
Step-by-Step Solution
Solution:
  1. Step 1: Understand constant usage in match expression

    Constants OK and ERROR are used as keys in match to return messages.
  2. Step 2: Call getMessage with Status::OK (1)

    Match returns 'Success' for code 1.
  3. Final Answer:

    Success -> Option D
  4. Quick Check:

    Constants used as keys return mapped string [OK]
Quick Trick: Use self::CONST inside static methods for constants [OK]
Common Mistakes:
  • Expecting constant name instead of mapped string
  • Confusing constant value with output
  • Not using self:: inside class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes