Challenge - 5 Problems
Interface Constants Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing interface constant
What is the output of this PHP code?
PHP
<?php interface Vehicle { const WHEELS = 4; } class Car implements Vehicle { public function wheels() { return self::WHEELS; } } $car = new Car(); echo $car->wheels();
Attempts:
2 left
💡 Hint
Remember that interface constants are public and can be accessed via the interface name.
✗ Incorrect
Interface constants are public and can be accessed using self:: inside the implementing class because they are inherited as class constants. self::WHEELS looks for the constant in Car, which implements Vehicle, so it finds WHEELS and returns 4. Use Vehicle::WHEELS or self::WHEELS to access the constant.
❓ Predict Output
intermediate2:00remaining
Value of interface constant in class
What will be the output of this PHP code?
PHP
<?php interface Status { const ACTIVE = 1; const INACTIVE = 0; } class User implements Status { public function getStatus() { return self::ACTIVE; } } $user = new User(); echo $user->getStatus();
Attempts:
2 left
💡 Hint
Check how interface constants are accessed inside implementing classes.
✗ Incorrect
Interface constants are public and can be accessed using self:: inside the implementing class because they are inherited as class constants. self::ACTIVE looks for the constant in User, which implements Status, so it finds ACTIVE and returns 1. Use Status::ACTIVE or self::ACTIVE to access the constant.
❓ Predict Output
advanced2:00remaining
Output when accessing interface constant via instance
What is the output of this PHP code?
PHP
<?php interface Shape { const SIDES = 3; } class Triangle implements Shape { public function sides() { return $this::SIDES; } } $triangle = new Triangle(); echo $triangle->sides();
Attempts:
2 left
💡 Hint
Interface constants must be accessed via the interface name.
✗ Incorrect
$this::SIDES uses late static binding to access the constant in the Triangle class, which implements Shape and inherits the constant SIDES. Therefore, it returns 3. Interface constants are accessible via self::, static::, or $this:: inside the implementing class.
❓ Predict Output
advanced2:00remaining
Output of accessing interface constant with same name in class
What will this PHP code output?
PHP
<?php interface Animal { const LEGS = 4; } class Dog implements Animal { const LEGS = 3; public function legs() { return self::LEGS; } public function animalLegs() { return Animal::LEGS; } } $dog = new Dog(); echo $dog->legs() . ',' . $dog->animalLegs();
Attempts:
2 left
💡 Hint
Class constants override interface constants with the same name when accessed via self.
✗ Incorrect
self::LEGS inside Dog refers to Dog::LEGS which is 3. Animal::LEGS accesses the interface constant which is 4. So output is '3,4'.
🧠 Conceptual
expert2:00remaining
Why interface constants cannot be overridden in PHP
Which statement best explains why interface constants in PHP cannot be overridden by implementing classes?
Attempts:
2 left
💡 Hint
Think about the purpose of interfaces and their constants in contracts.
✗ Incorrect
Interface constants define fixed values that all implementing classes must share to maintain a consistent contract. They are implicitly final and cannot be overridden to avoid breaking this contract.