📖 Scenario: You are building a simple PHP program to manage user roles in a system. Each role has a fixed access level represented by a number. You want to use interface constants to keep these access levels organized and easy to use.
🎯 Goal: Create an interface with constants for user roles and their access levels. Then, use these constants in a class to display the access level for a specific role.
📋 What You'll Learn
Create an interface called UserRoles with constants ADMIN, EDITOR, and VIEWER having values 3, 2, and 1 respectively.
Create a class called AccessControl with a method getAccessLevel that takes a role constant and returns its access level.
Use the interface constants inside the class method to return the correct access level.
Print the access level for the EDITOR role.
💡 Why This Matters
🌍 Real World
Interface constants help keep fixed values organized and easy to maintain in large PHP applications, such as user roles or configuration settings.
💼 Career
Understanding interface constants is useful for PHP developers working on projects that require clear, reusable, and consistent values across multiple classes.
Progress0 / 4 steps
1
Create the interface with constants
Create an interface called UserRoles with constants ADMIN set to 3, EDITOR set to 2, and VIEWER set to 1.
PHP
Hint
Use the interface keyword and define constants inside it using const NAME = value;.
2
Create the AccessControl class with getAccessLevel method
Create a class called AccessControl with a public method getAccessLevel that takes a parameter $role and returns the value of $role.
PHP
Hint
Define a class with a public method that returns the input parameter.
3
Use interface constants in the method call
Create an instance of AccessControl called $accessControl. Then call the method getAccessLevel on $accessControl with the argument UserRoles::EDITOR and store the result in a variable called $level.
PHP
Hint
Create an object with new and call the method using -> operator with the interface constant.
4
Print the access level
Print the value of the variable $level using echo.
PHP
Hint
Use echo $level; to display the access level.
Practice
(1/5)
1. What is the correct way to access a constant defined in a PHP interface named MyInterface with a constant VERSION?
easy
A. MyInterface->VERSION
B. $this->VERSION
C. self::VERSION
D. MyInterface::VERSION
Solution
Step 1: Understand interface constants access
Constants in interfaces are accessed using the interface name followed by double colon (::) and the constant name.
Step 2: Apply correct syntax
Using MyInterface::VERSION correctly accesses the constant defined in the interface.
Final Answer:
MyInterface::VERSION -> Option D
Quick Check:
Interface constants use InterfaceName::CONSTANT [OK]
Hint: Use InterfaceName::CONSTANT to access interface constants [OK]
Common Mistakes:
Using $this->CONSTANT inside interface context
Using self::CONSTANT which refers to class, not interface
Using arrow operator (->) with interface name
2. Which of the following is the correct way to declare a constant inside a PHP interface?
In PHP interfaces, constants are declared using const keyword without visibility modifiers.
Step 2: Identify correct syntax
const MAX_SIZE = 100; is the correct way. Using define() or visibility keywords is invalid in interfaces.
Final Answer:
const MAX_SIZE = 100; -> Option A
Quick Check:
Interface constants use const without visibility [OK]
Hint: Use const without public/private in interfaces [OK]
Common Mistakes:
Using define() inside interface
Adding public/private before const
Using var keyword for constants
3. Given the interface and class below, what will be the output?
interface Status {
const ACTIVE = 1;
const INACTIVE = 0;
}
class User implements Status {
public function getStatus() {
return Status::ACTIVE;
}
}
$user = new User();
echo $user->getStatus();
medium
A. ACTIVE
B. 0
C. 1
D. Error: Cannot access constant
Solution
Step 1: Understand interface constants usage
The interface Status defines constants ACTIVE = 1 and INACTIVE = 0. The class User implements this interface.
Step 2: Trace method getStatus()
The method returns Status::ACTIVE, which is the integer 1. The echo statement prints this value.
Final Answer:
1 -> Option C
Quick Check:
Interface constant value printed = 1 [OK]
Hint: Interface constants accessed as InterfaceName::CONSTANT return their value [OK]
Common Mistakes:
Expecting constant name string instead of value
Trying to access constant via $this
Assuming constants are not accessible in implementing class
4. What is wrong with the following PHP code?
interface Config {
public const TIMEOUT = 30;
}
medium
A. Interface constants must be assigned string values only
B. Constants in interfaces cannot have visibility modifiers
C. Constants must be declared as static in interfaces
In PHP, interface constants must be declared without visibility keywords like public, private, or protected.
Step 2: Identify the error in code
The code uses public const TIMEOUT = 30;, which is invalid syntax for interface constants.
Final Answer:
Constants in interfaces cannot have visibility modifiers -> Option B
Quick Check:
No visibility allowed on interface constants [OK]
Hint: Do not add public/private before const in interfaces [OK]
Common Mistakes:
Adding public or private before const in interface
Thinking constants must be static in interfaces
Assuming constants only accept strings
5. You want to define an interface Shape with constants CIRCLE = 1, SQUARE = 2, and TRIANGLE = 3. Then, create a class Circle implementing Shape that returns the shape type constant. Which code snippet correctly implements this?
hard
A. interface Shape {
const CIRCLE = 1;
const SQUARE = 2;
const TRIANGLE = 3;
}
class Circle implements Shape {
public function getType() {
return Shape::CIRCLE;
}
}
B. interface Shape {
public const CIRCLE = 1;
public const SQUARE = 2;
public const TRIANGLE = 3;
}
class Circle implements Shape {
public function getType() {
return $this->CIRCLE;
}
}
C. interface Shape {
const CIRCLE = 1;
const SQUARE = 2;
const TRIANGLE = 3;
}
class Circle implements Shape {
public function getType() {
return self::CIRCLE;
}
}
D. interface Shape {
const CIRCLE = 1;
const SQUARE = 2;
const TRIANGLE = 3;
}
class Circle implements Shape {
public function getType() {
return Shape->CIRCLE;
}
}
Solution
Step 1: Define interface constants correctly
Constants in interface Shape are declared using const without visibility modifiers.
Step 2: Access constants correctly in implementing class
Inside Circle, access the constant using Shape::CIRCLE. Using $this->CIRCLE or self::CIRCLE is incorrect because constants belong to the interface, not the class instance or class itself.
Final Answer:
Option A code snippet correctly implements interface constants and access -> Option A
Quick Check:
Interface constants accessed as InterfaceName::CONSTANT [OK]
Hint: Access interface constants with InterfaceName::CONSTANT, not $this or self [OK]
Common Mistakes:
Adding public before const in interface
Using $this->CONSTANT to access interface constant
Using self::CONSTANT inside class for interface constant
Using arrow operator (->) with interface name
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.