Interface constants in PHP - Time & Space Complexity
Let's see how using constants inside interfaces affects the time it takes for a program to run.
We want to know how the program's work changes when it uses these constants.
Analyze the time complexity of the following code snippet.
interface Status {
const ACTIVE = 1;
const INACTIVE = 0;
}
function checkStatus(int $status): string {
return $status === Status::ACTIVE ? 'Active' : 'Inactive';
}
$result = checkStatus(Status::ACTIVE);
This code defines constants inside an interface and uses them in a function to check a status value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing interface constants and a simple comparison.
- How many times: The function runs once here, so one comparison and one constant access.
Since the code only does a single check using constants, the work stays the same no matter how many constants exist.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 comparison, 1 constant access |
| 100 | 1 comparison, 1 constant access |
| 1000 | 1 comparison, 1 constant access |
Pattern observation: The number of operations does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the time to run this code stays the same no matter how many constants or inputs there are.
[X] Wrong: "Accessing interface constants takes longer as more constants are added."
[OK] Correct: Constants are fixed values stored in a way that accessing them is always quick and does not depend on how many constants exist.
Understanding that constants in interfaces are accessed quickly helps you explain how your code handles fixed values efficiently, a useful skill in many programming tasks.
"What if the function checked multiple constants in a loop? How would the time complexity change?"
Practice
MyInterface with a constant 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
UsingMyInterface::VERSIONcorrectly accesses the constant defined in the interface.Final Answer:
MyInterface::VERSION -> Option DQuick Check:
Interface constants use InterfaceName::CONSTANT [OK]
- Using $this->CONSTANT inside interface context
- Using self::CONSTANT which refers to class, not interface
- Using arrow operator (->) with interface name
Solution
Step 1: Recall interface constant declaration rules
In PHP interfaces, constants are declared usingconstkeyword without visibility modifiers.Step 2: Identify correct syntax
const MAX_SIZE = 100;is the correct way. Usingdefine()or visibility keywords is invalid in interfaces.Final Answer:
const MAX_SIZE = 100; -> Option AQuick Check:
Interface constants use const without visibility [OK]
- Using define() inside interface
- Adding public/private before const
- Using var keyword for constants
interface Status {
const ACTIVE = 1;
const INACTIVE = 0;
}
class User implements Status {
public function getStatus() {
return Status::ACTIVE;
}
}
$user = new User();
echo $user->getStatus();Solution
Step 1: Understand interface constants usage
The interfaceStatusdefines constantsACTIVE = 1andINACTIVE = 0. The classUserimplements this interface.Step 2: Trace method getStatus()
The method returnsStatus::ACTIVE, which is the integer 1. The echo statement prints this value.Final Answer:
1 -> Option CQuick Check:
Interface constant value printed = 1 [OK]
- Expecting constant name string instead of value
- Trying to access constant via $this
- Assuming constants are not accessible in implementing class
interface Config {
public const TIMEOUT = 30;
}Solution
Step 1: Review interface constant declaration rules
In PHP, interface constants must be declared without visibility keywords like public, private, or protected.Step 2: Identify the error in code
The code usespublic const TIMEOUT = 30;, which is invalid syntax for interface constants.Final Answer:
Constants in interfaces cannot have visibility modifiers -> Option BQuick Check:
No visibility allowed on interface constants [OK]
- Adding public or private before const in interface
- Thinking constants must be static in interfaces
- Assuming constants only accept strings
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?Solution
Step 1: Define interface constants correctly
Constants in interfaceShapeare declared usingconstwithout visibility modifiers.Step 2: Access constants correctly in implementing class
InsideCircle, access the constant usingShape::CIRCLE. Using$this->CIRCLEorself::CIRCLEis 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 AQuick Check:
Interface constants accessed as InterfaceName::CONSTANT [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
