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
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

Class Constants – PHP OOP

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 const keyword
  • 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

FeatureClass ConstantsClass Properties
Declared withconstvar, public, private
Requires $ symbolNoYes
Can change?NoYes
Accessed via::->
Memory allocationOne-timePer 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


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.

Related Article
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

PHP Tutorial – Complete Guide for Beginners to Advanced Welcome to the most comprehensive PHP tutorial available online at PHPOnline.in Read more

Introduction of PHP

Introduction to PHP – Learn PHP from Scratch with Practical Examples Welcome to your complete beginner's guide to PHP. Whether Read more

Syntax Overview of PHP

Syntax Overview of PHP (2025 Edition) Welcome to phponline.in, your one-stop platform for mastering PHP. This comprehensive, SEO-rich tutorial on Read more

Environment Setup in PHP

Setting Up PHP Environment (Beginner’s Guide) If you’re planning to learn PHP or start developing websites using PHP, the first Read more

Variable Types in PHP

PHP Variable Types: Complete Beginner's Guide to PHP Data Types Welcome to phponline.in, your trusted source for beginner-to-advanced level PHP Read more

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments