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 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
How to add MySQL alternate port and sockets/pipes in WordPress?WordPressWordPress: How to add a Search Icon in Menus with toggle effect using jQuery?jQueryLearn to make a responsive gallery using CSS Grid and without media queriesCSSWordPress: How to access first and random row values from a repeater field in ACF?WordPressSolution to Precision Problem in JavaScript NumbersJavaScript10 Usability Blunders to AvoidUI/UXVariables scope and shadowing in SCSS (Sass)SCSSWordPress plugin development: How to fix a SQL injection?WordPressHow to import a CSS file using PHP code and not HTML code?PHPSolution to “NullInjectorError: No provider for module” in Angular 9AngularHow to add CurrencyPipe in TypeScript file in Angular 9 Project?AngularHow to convert an object from API to JSON array in Angular 9?Angular