0
0
PHPprogramming~20 mins

Interface constants in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface Constants Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
A4
BError: Undefined constant WHEELS
C0
DFatal error: Access to interface constant via self
Attempts:
2 left
💡 Hint
Remember that interface constants are public and can be accessed via the interface name.
Predict Output
intermediate
2: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();
AError: Undefined constant ACTIVE
B0
C1
DFatal error: Cannot access interface constant via self
Attempts:
2 left
💡 Hint
Check how interface constants are accessed inside implementing classes.
Predict Output
advanced
2: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();
A0
BError: Cannot access interface constant via instance
CFatal error: Accessing constant SIDES via instance
D3
Attempts:
2 left
💡 Hint
Interface constants must be accessed via the interface name.
Predict Output
advanced
2: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();
A4,4
B3,4
C3,3
D4,3
Attempts:
2 left
💡 Hint
Class constants override interface constants with the same name when accessed via self.
🧠 Conceptual
expert
2:00remaining
Why interface constants cannot be overridden in PHP
Which statement best explains why interface constants in PHP cannot be overridden by implementing classes?
AInterface constants are implicitly final and shared across all implementations to ensure consistent contract values.
BImplementing classes can override interface constants freely without restrictions.
CPHP does not allow constants in interfaces at all, so overriding is impossible.
DInterface constants are private by default, so they cannot be accessed or overridden.
Attempts:
2 left
💡 Hint
Think about the purpose of interfaces and their constants in contracts.