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_name::CONSTANT

Class constants can be simple like this:


<?php
class Greetings {
  const MESSAGE = "Hello World!";
}

echo Greetings::MESSAGE; //Hello World!
?>


You can also define it inside a method:


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

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



Class constant expression example


<?php
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.


<?php
class Foo {
    public const BAR = 'bar';
    private const BAZ = 'baz';
}
echo Foo::BAR; //bar
echo Foo::BAZ; //Fatal error
?>



Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to add a select dropdown in React?ReactHow to display modal from bottom to top using CSS and JavaScript?CSSWordPress: How to add a Search Icon in Menus with toggle effect using jQuery?jQueryHow to use @if and @else in SCSS?SCSSA list of wp-cli commands to use via SSHWordPressWhat is the correct way to enqueue multiple CSS files in WordPress?WordPressHere’s what we can do with PHP date() functionPHPSelf-Driving and Intelligent NetworksMiscellaneousHow to select multiple values in React dropdown?ReactJavaScript Number MethodsJavaScriptHow to obfuscate JavaScript code to hide it from View Source?JavaScriptHow to Skip or Exclude a Specific URL or File From .htaccess Rewrite Rule?PHP