dots Created with Sketch.
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
?>


amazon

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

#Jul 22 Is there a CSS parent selector? #Jul 22 Difference between :where and :is in CSS #Jul 22 Does :is() pseudo selector hint at CSS preprocessing in the future? #Jul 22 Control Scrolling with CSS Scroll Snap #Jul 21 Control rendering using CSS content-visibility property #Jul 21 How to use @supports rule in CSS?
You might also like these
How to check if checkbox is checked or not using JavaScript?JavaScript$this Keyword in PHPPHPCasting in PostgreSQLPostgresWhat are Class Constants in PHP?PHPLearn how to add Scroll Indicator using CSS and JavaScript?CSSHow to create a Random Hex Color generator using JavaScript?JavaScriptPagination in CSS with multiple examplesCSSAvoid using new Array() in JavaScriptJavaScriptWhere is the PHP log file located on Mac OS?PHPContent Blocks in SCSS (Sass)SCSSHow to Remove PHP File Extensions From Your Website URLs?PHPWhy do we need HAVING Clause in SQL?SQL/MySQL