Table of Contents:
Class Constants in PHP OOP – Complete Guide With Examples
Class constants play a crucial role in Object-Oriented Programming by allowing you to define values that should never change throughout the lifecycle of a PHP application. Constants are extremely useful when you want to store fixed values such as configuration labels, status codes, roles, error messages, and reusable identifiers in a secure and readable format.
This guide explains everything you need to know about Class Constants in PHP OOP, including syntax, declaration rules, use cases, and real-world examples.
What Are Class Constants in PHP?
A Class Constant is a value defined inside a class that cannot be changed once declared.
They are declared using the const keyword.
Unlike variables:
- Class constants do not use the
$sign - Their values remain fixed
- They are accessed using the scope resolution operator (::)
- They can be accessed without creating an object
Example:
class MyClass {
const VERSION = "1.0";
}
Why Use Class Constants in PHP OOP?
Class constants help you store fixed, unchangeable values that increase code readability and reduce errors. They are widely used in real-world applications for:
- API versioning
- User roles
- Error messages
- Application modes (LIVE, TEST)
- Database status flags
- System-wide constant values
Using class constants enhances performance, security, and consistency throughout large PHP projects.
How to Declare Class Constants in PHP (Best Example for Beginners)
To create a class constant:
class MathValues {
const PI = 3.14;
}
Points to remember:
- Use
constkeyword - Names are usually written in uppercase
- Values cannot change after declaration
How to Access Class Constants Using the Scope Resolution Operator
Class constants are accessed using the double colon (::) operator.
Example:
echo MathValues::PI;
Output:3.14
You do not need to create an object:
$obj = new MathValues();
echo $obj::PI; // Valid but not recommended
Difference Between Class Constants and Class Properties
| Feature | Class Constants | Class Properties |
|---|---|---|
| Declared with | const | var, public, private |
Requires $ symbol | No | Yes |
| Can change? | No | Yes |
| Accessed via | :: | -> |
| Memory allocation | One-time | Per object |
PHP class constants, PHP OOP constants, const keyword in PHP, PHP scope resolution operator, PHP OOP tutorial, how to use class constants in PHP, PHP constant examples, PHP static constant, PHP OOP guide
Using Class Constants Inside Methods
You can access class constants inside class methods using self::.
Example:
class AppConfig {
const APP_NAME = "PHPOnline";
public function showAppName() {
return self::APP_NAME;
}
}
$config = new AppConfig();
echo $config->showAppName();
Class Constant Visibility in PHP (public, private, protected)
PHP 7.1+ allows constants to have visibility:
class Demo {
public const A = 1;
private const B = 2;
protected const C = 3;
}
Rules:
- public → accessible everywhere
- private → only inside the same class
- protected → accessible in child classes
Real-World Example Using Class Constants in PHP OOP
class OrderStatus {
const PENDING = "pending";
const PROCESSING = "processing";
const COMPLETED = "completed";
const CANCELLED = "cancelled";
}
echo OrderStatus::COMPLETED;
Use cases:
- eCommerce order statuses
- Payment response codes
- User access roles
- API message types
Using Class Constants in Inheritance (Advanced PHP OOP Topic)
Class constants can be inherited just like methods and properties.
class ParentClass {
const MESSAGE = "Welcome!";
}
class ChildClass extends ParentClass {}
echo ChildClass::MESSAGE;
Overriding Class Constants in Child Classes
PHP allows child classes to redefine constants:
class Base {
const ROLE = "USER";
}
class Admin extends Base {
const ROLE = "ADMIN";
}
echo Admin::ROLE;
Best Practices for Using Class Constants in PHP OOP
- Use uppercase names separated with underscores
- Group constants logically inside classes
- Use constants for fixed configuration values
- Avoid magic strings inside code
- Prefer constants over global variables
- Use visibility modifiers when needed
- Use class constants for enums (PHP 8.1+ offers native enums)
Learn more
- Learn more about PHP OOP Concepts on our homepage
- Read next: Class Properties in PHP OOP
- Explore: Static Methods and Properties in PHP
Frequently Asked Questions (FAQ)
1. What is a class constant in PHP?
A class constant is a fixed, unchangeable value defined using the const keyword inside a class.
2. How do I access a class constant?
Using the scope resolution operator:ClassName::CONSTANT_NAME
3. Can class constants have visibility?
Yes. PHP supports public, private, and protected visibility for class constants.
4. Can I change a class constant after declaration?
No. Class constants are immutable.
5. Do I need to create an object to access a constant?
No. Class constants can be accessed directly without creating an object.
6. Can class constants be used inside methods?
Yes. Use self::CONSTANT_NAME inside class methods.
7. Can child classes override class constants?
Yes, but only by redeclaring them in the child class.

