Quick Guide To Class Constants In PHP

In this article, we will explore the concept of PHP class constants, how they work, and why they are useful. We will also see some examples of how we can use them to help us with our programming projects in the future.

The term Class constant in PHP Object-Oriented Programming (OOP) refers to a data member that is defined in the class and whose value cannot be changed while the class is running.

The use of PHP class constants can make it easier and more efficient for you to manage data that is shared across all of the instances (objects) of a class in the same way.



PHP Class Constants – What are they?

In PHP, a class constant is a value that is associated with a class rather than with a particular instance of the class.

PHP class constants are similar to regular constants, but they are associated with a specific class and can be accessed using the class name.

Once a constant is declared, it cannot be changed.

It’s case-sensitive. Despite this, it is preferred that constant names be given in all uppercase.

When you have to define some constant data within a class, you can use class constants as a way to define that data.

Class constants are defined using the const keyword followed by the constant name and value.

Here is an example:

class MrxClass {
const MY_CONSTANT = 'Hello, Developer!';
}

Above we have defined a class called MrxClass with a constant named MY_CONSTANT and a value of “Hello, Developer!”.

Remember: PHP class constants are typically written in all uppercase letters.

Accessing PHP Class Constants

In order to access a constant from outside the class, we must use the class name along with the scope resolution operator (::) followed by the name of the constant, like in the following example:

Example: 

<?php class Greeting { const WELCOME_MSG = "Welcome to mrexamples.com!"; } echo Greeting::WELCOME_MSG; ?>
Below is another example of using class constant and accessing outside the class:

Example: 

<?php class Circle{ const PI=M_PI; function areaOfCircle($radius){ $area_of_circle=($radius**2)*(self::PI); return $area_of_circle; } } $aoc=new Circle(); echo "PI=". Circle::PI . "<br>";//Printing const value outside the class echo "Area of Circle is=" . $aoc->areaOfCircle(3); ?>
You may also access a constant from within a class by using the self keyword followed by the scope resolution operator (::) followed by the constant name, as shown in the example below:

Example: 

<?php class Greeting { const WELCOME_MSG = "Welcome to mrexamples.com!"; public function welcome() { echo self::WELCOME_MSG; } } $greeting = new Greeting(); $greeting->welcome(); ?>
Below is another example for solid understanding of using class constant and displaying inside the class:

Example: 

<?php class Circle{ const PI=M_PI; function areaOfCircle($radius){ echo "PI=". Circle::PI . "<br>"; //Printing const value inside the class $area_of_circle=($radius**2)*(self::PI); return $area_of_circle; } } $aoc=new Circle(); //round() is used to round float values with 2 decimal places echo "Area of Circle is= " . round($aoc->areaOfCircle(3), 2); ?>

Example Explanation

Above example defines a class named Circle that has a constant named PI with a value of M_PI, which is a built-in PHP constant for the mathematical constant pi (3.1415926535898…).
The class also has a function named “areaOfCircle” that takes a parameter named “$radius” and calculates the area of a circle using the formula (pi x r^2), where “pi” is the value of the constant “PI” and “r” is the radius.
Inside the “areaOfCircle” function, the constant “PI” is accessed using the scope resolution operator (::) and printed using the “echo” statement.
The “self” keyword is used to refer to the class itself, and the constant “PI” is accessed using “self::PI” instead of “Circle::PI” inside the “areaOfCircle” function.
Finally, an instance of the “Circle” class is created and the “areaOfCircle” function is called with a radius value of 3. The result is rounded to two decimal places using the “round” function and printed using the echo statement.
Overall, this code demonstrates how to use a class constant to store a commonly used value (pi) in a PHP class and how to access and use it inside a class function to perform a calculation.

PHP Class Constants vs. Class Properties

  • PHP class constants cannot be changed once defined, while class properties can be modified throughout the lifetime of an object.
  • Class constants are associated with a specific class, while class properties are associated with instances of the class.
  • Class constants are useful for storing values that should not be changed during script execution, such as configuration values or API keys.
  • PHP class constants provide a way to name and organize values that are associated with a specific class, making code easier to read and understand.

PHP Class Constants and Magic Constants

PHP also has a set of predefined constants known as magic constants, which provide information about the current script’s environment.

It is important to note that magic constants are not class constants, and they cannot be accessed using the double colon :: notation.

Here is a list of some of the most commonly used magic constants:

Magic ConstantsOverview
LINEThe current line number of the file.
FILEThe full path and filename of the file.
DIRThe directory of the file.
FUNCTIONThe name of the current function.
CLASSThe name of the current class.
TRAITThe name of the current trait.
METHODThe name of the current method.
NAMESPACEThe name of the current namespace.
Please subscribe to our newsletter below in order to stay up to date with all the latest developments in PHP and to learn more about the language.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *