Bird
Raised Fist0
PHPprogramming~10 mins

Constants in classes in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Constants in classes
Define class with constant
↓
Access constant via ClassName::CONSTANT
↓
Use constant value in code
↓
Output or use constant value
↓
End
This flow shows how a class constant is defined, accessed using ClassName::CONSTANT, and then used in the program.
Execution Sample
PHP
<?php
class Car {
  const WHEELS = 4;
}
echo Car::WHEELS;
?>
Defines a class Car with a constant WHEELS and prints its value.
Execution Table
StepActionCode LineEvaluationOutput
1Define class Car with constant WHEELS=4class Car { const WHEELS = 4; }Class Car created with constant WHEELS=4
2Access constant WHEELS from CarCar::WHEELSValue 4 retrieved
3Print constant valueecho Car::WHEELS;Output 44
4End of script
💡 Script ends after printing the constant value 4.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Car::WHEELSundefined4444
Key Moments - 2 Insights
Why do we use ClassName::CONSTANT instead of $this->CONSTANT?
Class constants are accessed with ClassName::CONSTANT because they belong to the class itself, not to an instance. This is shown in execution_table step 2 where Car::WHEELS is used.
Can we change the value of a class constant after defining it?
No, class constants cannot be changed after definition. The execution_table shows the constant value stays 4 throughout.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of Car::WHEELS at step 2?
Anull
Bundefined
C4
D0
💡 Hint
Check the 'Evaluation' column at step 2 in the execution_table.
At which step does the program output the constant value?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the 'Output' column in the execution_table.
If we tried to change Car::WHEELS after definition, what would happen?
AThe value changes successfully
BThe value remains unchanged
CA syntax error occurs
DThe program outputs the new value
💡 Hint
Refer to key_moments about immutability of class constants.
Concept Snapshot
Class constants are fixed values inside a class.
Define with 'const NAME = value;'.
Access with ClassName::NAME.
Cannot be changed after definition.
Useful for fixed data related to the class.
Full Transcript
This example shows how to define and use constants inside PHP classes. We define a class Car with a constant WHEELS set to 4. We access this constant using Car::WHEELS and print its value. Class constants belong to the class itself, not instances, so we use :: to access them. Constants cannot be changed after they are set. The execution table traces each step from defining the class, accessing the constant, printing it, and ending the script. Key moments clarify why we use ClassName::CONSTANT and that constants are immutable. The quiz tests understanding of these points.

Practice

(1/5)
1. What keyword is used to declare a constant inside a PHP class?
easy
A. let
B. var
C. const
D. static

Solution

  1. Step 1: Understand constant declaration in PHP classes

    In PHP, constants inside classes are declared using the const keyword, which defines a fixed value that cannot change.
  2. Step 2: Differentiate from other keywords

    var is used for properties (deprecated), let is not a PHP keyword, and static is for static properties or methods, not constants.
  3. Final Answer:

    const -> Option C
  4. Quick Check:

    Class constants use const keyword [OK]
Hint: Use 'const' to declare fixed values in classes [OK]
Common Mistakes:
  • Using 'var' instead of 'const'
  • Trying to use 'let' which is not PHP
  • Confusing 'static' with constants
2. Which of the following is the correct syntax to access a class constant named VERSION in PHP?
easy
A. ClassName::VERSION
B. $obj->VERSION
C. ClassName->VERSION
D. $obj:VERSION

Solution

  1. Step 1: Recall how to access class constants

    Class constants are accessed using the scope resolution operator :: with the class name, like ClassName::CONSTANT.
  2. Step 2: Identify incorrect options

    $obj->VERSION tries to access a property, not a constant; ClassName->VERSION is invalid syntax; $obj:VERSION uses single colon which is invalid syntax.
  3. Final Answer:

    ClassName::VERSION -> Option A
  4. Quick Check:

    Access constants with ClassName::CONSTANT [OK]
Hint: Use ClassName::CONSTANT to access class constants [OK]
Common Mistakes:
  • Using object operator -> instead of ::
  • Trying to access constants as properties
  • Confusing variable and class name usage
3. What will be the output of the following PHP code?
class Car {
    const WHEELS = 4;
}
echo Car::WHEELS;
medium
A. 4
B. WHEELS
C. Error: Undefined constant
D. null

Solution

  1. Step 1: Identify the constant declaration

    The class Car declares a constant WHEELS with value 4.
  2. Step 2: Understand the echo statement

    The code prints the value of Car::WHEELS, which is 4.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    Class constant value printed = 4 [OK]
Hint: ClassName::CONSTANT prints the constant's value [OK]
Common Mistakes:
  • Expecting the name 'WHEELS' to print
  • Thinking it causes an error
  • Confusing constants with variables
4. What is wrong with this PHP code?
class Book {
    const TITLE = 'PHP Basics';
}

$book = new Book();
echo $book::TITLE;
medium
A. Cannot access constant via object instance with ::
B. Constant TITLE is not declared correctly
C. Missing semicolon after constant declaration
D. Code will output 'PHP Basics' without error

Solution

  1. Step 1: Verify constant declaration

    The class Book correctly declares const TITLE = 'PHP Basics';.
  2. Step 2: Verify access syntax

    $book = new Book(); creates an instance, and $book::TITLE is valid syntax to access the class constant from an instance. The code outputs PHP Basics without error.
  3. Final Answer:

    Code will output 'PHP Basics' without error -> Option D
  4. Quick Check:

    $instance::CONSTANT is valid [OK]
Hint: ClassName::CONSTANT or $instance::CONSTANT both valid [OK]
Common Mistakes:
  • Using -> for constants instead of ::
  • Declaring with var instead of const
  • Thinking $instance::CONSTANT is invalid
5. Given the class below, which code correctly prints the constant STATUS without creating an object?
class Server {
    const STATUS = 'Online';
}
hard
A. echo Server->STATUS;
B. echo Server::STATUS;
C. echo $server::STATUS;
D. echo Server::status;

Solution

  1. Step 1: Identify correct constant access syntax

    Constants are accessed using the class name and the scope resolution operator :: with the exact constant name in uppercase.
  2. Step 2: Evaluate each option

    Server->STATUS is invalid because -> is for object properties; $server::STATUS is invalid because $server is undefined and constants should be accessed via class name; Server::status is wrong due to case sensitivity; only Server::STATUS is correct.
  3. Final Answer:

    echo Server::STATUS; -> Option B
  4. Quick Check:

    Use ClassName::CONSTANT with exact case [OK]
Hint: Use ClassName::CONSTANT with exact uppercase name [OK]
Common Mistakes:
  • Using object operator -> instead of ::
  • Ignoring case sensitivity in constant names
  • Trying to access constants via undefined variables
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