Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
PHP - Interfaces and Traits
What will be the output of this code?
interface Status {
  const OK = 1;
}
class Response implements Status {
  public function getStatus() {
    return Status::OK;
  }
}
$response = new Response();
echo $response->getStatus();
AStatus::OK
BError: Cannot access constant
C1
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Access interface constant using interface name

    Inside getStatus(), Status::OK correctly accesses the constant value 1.
  2. Step 2: Echo the returned value

    Calling getStatus() returns 1, which is printed by echo.
  3. Final Answer:

    1 -> Option C
  4. Quick Check:

    Interface constants accessed by InterfaceName::CONSTANT [OK]
Quick Trick: Use InterfaceName::CONSTANT to access constants inside methods [OK]
Common Mistakes:
  • Expecting error accessing interface constant
  • Using $this::OK instead of Status::OK
  • Confusing constant with string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes