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 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

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 implement NSNumberFormatter in Swift for currency?SwiftCurrencyPipe in Angular 9 with ExamplesAngularIN Operator in PostgreSQLPostgresMiddleware in NextJSNextJSHow services and dependency injection work in Angular?AngularIs Software Really Eating The World? I don’t think soMiscellaneousHow to float an image around texts?CSSWhat does it mean by Continuous Integration, Continuous Delivery and Continuous Deployment?MiscellaneousHow to link/add CSS file to HTML Document?CSSRelative Length Units in CSSCSSHow to disable scrolling on html body on menu click using JavaScript?JavaScriptThe ALPHA function in Envision BasicEnvision Basic