Bird
0
0

Given the interface and classes below, what will be the output?

hard📝 Application Q8 of 15
PHP - Interfaces and Traits
Given the interface and classes below, what will be the output?
interface Levels {
  const LOW = 1;
  const HIGH = 10;
}
class Base implements Levels {
  public function getLow() {
    return self::LOW;
  }
}
class Advanced extends Base {
  public function getHigh() {
    return Levels::HIGH;
  }
}
$adv = new Advanced();
echo $adv->getLow() + $adv->getHigh();
A11
BError: Undefined constant LOW
CError: Cannot access constant HIGH
D1
Step-by-Step Solution
Solution:
  1. Step 1: Understand constant inheritance and access

    Base implements Levels, so self::LOW in Base refers to Levels::LOW (1).
  2. Step 2: Analyze Advanced class method

    getHigh() returns Levels::HIGH (10) directly.
  3. Step 3: Calculate output

    Sum of getLow() + getHigh() = 1 + 10 = 11.
  4. Final Answer:

    11 -> Option A
  5. Quick Check:

    Interface constants accessible via self:: and InterfaceName:: [OK]
Quick Trick: Interface constants inherited and accessible via self:: or InterfaceName:: [OK]
Common Mistakes:
  • Expecting errors accessing constants in child class
  • Confusing self:: with static::
  • Forgetting constants are inherited

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes