Bird
Raised Fist0
PHPprogramming~10 mins

Interface constants in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Interface constants
Define Interface with Constants
↓
Implement Interface in Class
↓
Access Constants via Interface or Class
↓
Use Constant Values in Code
This flow shows how to define constants inside an interface, implement the interface in a class, and access those constants.
Execution Sample
PHP
<?php
interface MyInterface {
  const VERSION = "1.0";
}
class MyClass implements MyInterface {}
echo MyClass::VERSION;
?>
Defines an interface with a constant, implements it in a class, and prints the constant value.
Execution Table
StepActionEvaluationResult
1Define interface MyInterface with constant VERSIONconst VERSION = "1.0"Interface MyInterface created with VERSION = "1.0"
2Define class MyClass implementing MyInterfaceclass MyClass implements MyInterface {}Class MyClass created, inherits interface constants
3Access constant VERSION via MyClass::VERSIONMyClass::VERSION"1.0"
4Print constant valueecho MyClass::VERSIONOutput: 1.0
💡 Program ends after printing the constant value.
Variable Tracker
VariableStartAfter 1After 2After 3Final
MyInterface::VERSIONundefined"1.0""1.0""1.0""1.0"
MyClass::VERSIONundefinedundefined"1.0""1.0""1.0"
Key Moments - 2 Insights
Why can we access the constant using the class name even though the constant is defined in the interface?
Because when a class implements an interface, it inherits the interface's constants, so MyClass::VERSION is valid as shown in execution_table step 3.
Can interface constants be changed after definition?
No, interface constants are always public, static, and final, so their values cannot be changed after definition, as implied by the constant's fixed value in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of MyClass::VERSION?
A"VERSION"
B"1.0"
Cundefined
Dnull
💡 Hint
Refer to execution_table row 3 where MyClass::VERSION evaluates to "1.0".
At which step does the program output the constant value?
AStep 4
BStep 1
CStep 3
DStep 2
💡 Hint
Check execution_table row 4 where echo prints the output.
If the interface constant VERSION was changed to "2.0", what would MyClass::VERSION output?
A"1.0"
BError
C"2.0"
Dnull
💡 Hint
Interface constants are inherited; changing VERSION in interface changes the value accessed by the class, see variable_tracker.
Concept Snapshot
Interface constants in PHP are defined inside interfaces using const keyword.
They are public, static, and final by default.
Classes implementing the interface inherit these constants.
Access constants via InterfaceName::CONSTANT or ClassName::CONSTANT.
Constants cannot be changed after definition.
Full Transcript
This example shows how to define constants inside a PHP interface and access them through a class that implements the interface. First, the interface MyInterface is defined with a constant VERSION set to "1.0". Then, a class MyClass implements this interface. Because interface constants are inherited, MyClass can access VERSION using MyClass::VERSION. Finally, the program prints the value "1.0". Interface constants are always public, static, and final, so their values cannot be changed after definition. This makes them useful for fixed values shared across classes.

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

  1. Step 1: Understand interface constants access

    Constants in interfaces are accessed using the interface name followed by double colon (::) and the constant name.
  2. Step 2: Apply correct syntax

    Using MyInterface::VERSION correctly accesses the constant defined in the interface.
  3. Final Answer:

    MyInterface::VERSION -> Option D
  4. 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?
easy
A. const MAX_SIZE = 100;
B. define('MAX_SIZE', 100);
C. public const MAX_SIZE = 100;
D. var MAX_SIZE = 100;

Solution

  1. Step 1: Recall interface constant declaration rules

    In PHP interfaces, constants are declared using const keyword without visibility modifiers.
  2. Step 2: Identify correct syntax

    const MAX_SIZE = 100; is the correct way. Using define() or visibility keywords is invalid in interfaces.
  3. Final Answer:

    const MAX_SIZE = 100; -> Option A
  4. 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

  1. Step 1: Understand interface constants usage

    The interface Status defines constants ACTIVE = 1 and INACTIVE = 0. The class User implements this interface.
  2. Step 2: Trace method getStatus()

    The method returns Status::ACTIVE, which is the integer 1. The echo statement prints this value.
  3. Final Answer:

    1 -> Option C
  4. 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
D. Constants cannot be declared inside interfaces

Solution

  1. Step 1: Review interface constant declaration rules

    In PHP, interface constants must be declared without visibility keywords like public, private, or protected.
  2. Step 2: Identify the error in code

    The code uses public const TIMEOUT = 30;, which is invalid syntax for interface constants.
  3. Final Answer:

    Constants in interfaces cannot have visibility modifiers -> Option B
  4. 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

  1. Step 1: Define interface constants correctly

    Constants in interface Shape are declared using const without visibility modifiers.
  2. 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.
  3. Final Answer:

    Option A code snippet correctly implements interface constants and access -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard