Yogesh Chauhan's Blog

What are Class Constants in PHP?

in PHP on June 16, 2021

Class constants are a bit different than normal(?) constants. Mostly they are used to declare some constant data inside a class.

You can define class constants inside a class with a const keyword and they’ll stay unchangeable. They can be redefined by the child class though.

Constant identifiers are case-sensitive but they are always defined in uppercase.

By default, the visibility of class constants if public.

You can have constants in interfaces too!

You can reference a class using a variable but the value of the variable can not be a keyword like self, static or parent.

Class constants are allocated once per class, and not for each class instance.

Class Constant Examples

You can use the scope resolution operator (::) to access the class constant. For e.g.


Class constants can be simple like this:

class Greetings {
  const MESSAGE = "Hello World!";

echo Greetings::MESSAGE; //Hello World!

You can also define it inside a method:

class Greetings {
  const MESSAGE = "Hello World!";
  function showGreetings() {
     echo  self::MESSAGE;

$g = new Greetings();
$g->showGreetings(); //Hello World!

Class constant expression example

const ONE = 1;
class foo {
    const TWO = ONE * 2;
    const THREE = ONE + self::TWO;
    const SENTENCE = 'The value of THREE is '.self::THREE;

echo foo::SENTENCE; 
//The value of THREE is 3

Class constants with visibility

From PHP 7.1.0, you can define class constants with visibility (access) modifiers.

class Foo {
    public const BAR = 'bar';
    private const BAZ = 'baz';
echo Foo::BAR; //bar
echo Foo::BAZ; //Fatal error

Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Learn to create profile card using HTML and CSSCSSJavaScript: how to detect a browser using the user agent?JavaScriptWindow setInterval() Method in JavaScriptJavaScriptThe SQL UNION OperatorSQL/MySQLStyling Lists with CSSCSSIs monitoring an employee’s work on a computer a desirable or undesirable activity?MiscellaneousIntroduction to components and templates Part 4: Pipes and DirectivesAngularHow to Hide HTML elements automatically after few seconds using JavaScript?HTMLHow to add and remove capabilities from a role in WordPress?WordPressHow to create ‘share on LinkedIn’ link using just HTML?HTMLHow to define variables in SCSS (Sass)?SCSSKilling A Project Part 1: What criteria should be used to cancel/kill a project?Miscellaneous