Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
PHP - Interfaces and Traits
Consider this code:
interface Config {
  const DEFAULT = 'none';
}
class Settings implements Config {
  public function getDefault() {
    return self::DEFAULT;
  }
}
class CustomSettings extends Settings {
  const DEFAULT = 'custom';
}
$custom = new CustomSettings();
echo $custom->getDefault();

What will be the output?
Acustom
Bnull
CError: Cannot redeclare constant DEFAULT
Dnone
Step-by-Step Solution
Solution:
  1. Step 1: Understand constant resolution in inheritance

    getDefault() uses self::DEFAULT in Settings, so it refers to Settings::DEFAULT or inherited constant.
  2. Step 2: Analyze constant in CustomSettings

    CustomSettings declares its own DEFAULT constant, but getDefault() is defined in Settings and uses self::DEFAULT there.
  3. Step 3: Determine output

    Since getDefault() uses self::DEFAULT in Settings, it returns Config::DEFAULT value 'none'.
  4. Final Answer:

    none -> Option D
  5. Quick Check:

    self:: in parent method refers to parent's class constants [OK]
Quick Trick: self:: uses class where method is defined, not child class [OK]
Common Mistakes:
  • Expecting child's constant to override parent's self:: reference
  • Confusing self:: with static::
  • Assuming redeclaration causes error here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes