🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Complete Tutorials of PHP OOP Constants with Example code

In this Tutorial, we are going to learn about Complete Tutorials of PHP OOP Constants with Example code. Through this we will learn about the concept about Constants.  But first let me give you a brief explanation of what is Object-oriented programming (OOP)?

What is Object-oriented programming (OOP)?

Object-oriented programming consists of combining a set of variables (properties) and functions (methods), which are referred to as an object. These things are arranged into classes in which individual items can be combined. OOP can enable you to consider the objects and the many activities in connection with the objects in a program’s code. For more details you can go on my previous article Read More. [Object-oriented programming (OOP) Concept Simplified!]

What is Constants in PHP?

Constant Types-PHP

A constant is a variable that cannot be modified, exactly as the name indicates. If you define a constant, you assign it a value, and the value never changes thereafter. Simple variables are normally easier to operate, but, in certain situations, constants, such as signals to other programmers (or yourself, should you forget), are preferred not to modify this particular value while running.

Class constants are similar to normal constants, except that they are defined in a class and accessible through this particular class. You use the double-colon operator to access a constant class, as with static members.

A class constant is declared inside a class with the const keyword.

We can access a constant from outside the class by using the class name followed by the scope resolution operator (::) followed by the constant name,

<?php
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com!";
}
echo Goodbye::LEAVING_MESSAGE;
?>

Which are the points we have to know about the Constants?

PHP constants | PHP Constants Types | How to Define a Constant in php
  • Constants are one variable type that we may classify with const for any class.
  • The value cannot be modified after assignment of these variables.
  • Class constants are distinct from conventional variables, as the class constants do not require $ to declare.
  • If we are inside the class then values of the constants can be get using self keyword, but accessing the value outside the class you have to use Scope Resolution Operator.
  • A particular entity which is fixed on a class basis.
  • Constant can also be used for interfaces.
  • The class name can truly be a variable when a constant of a class is called using the ($)classname (::) syntax.

How to create a PHP Constants?

Php constants class 336

The define() function in PHP is used to create a constant

define(name, value, case_insensitive)

There are some of The parameters are as follows:-

  • name: The name of the constant.
  • value: The value to be stored in the constant.
  • case_insensitive: Defines whether a constant is case insensitive. By default this value is False, i.e., case sensitive.
define("PI",3.14);

The constant is specified as a key value pair in the above programmer. The value of the constant is obtained with the key.

In the program above, just two parameters are provided to define(). Optionally, the define() provides a third parameter to set a constant’s case insensitivity. The constants are sensitive to cases by default.

Example:-

<?php
// This creates a case-sensitive constant
define("WELCOME", "GeeksforGeeks");
echo WELCOME, "\n";
// This creates a case-insensitive constant
define("HELLO", "GeeksforGeeks", true);
echo hello;
?>

Output:-

The constant() function in PHP

Another technique to output constants using the constant() function is not to use the echo statement.

PHP constant() is employed as an input for this method to retrieve the value of the constant. The sample code below use constant() to obtain the constant value by using the identification.

constant(name)

Example:-

<?php
define("WELCOME", "GeeksforGeeks!!!");
echo WELCOME, "\n";
echo constant("WELCOME");
// same as previous
?>

Output:-

PHP Constants with const

A construct in which define() is a function is a PHP const. In order to initialize constants, we need use the static name and value with a const statement where define() permits interpolation and function calling while declaring the constant. This constriction is always case sensitive, specified in this construct.

Setting constants with const is beneficial in the aspect of readability; nevertheless, define() has several functions, which enables case sensitivity, allowing function call values to be assigned to the constant, which reveals constants at runtime.

The following line illustrates a const statement as an example of initialization.

const PI = "3.14";
echo PI; // Output: 3.14

Some Examples!

Basic of PHP

Example 1:-

<?php
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting DevopsSchool.com!";
public function byebye() {
echo self::LEAVING_MESSAGE;
}
}
$goodbye = new Goodbye();
$goodbye->byebye();
?>

Output:-

Example 2:-

<?php
//create class
class demo
{
//create constant variable
const a= 10;
}
//call constant variable.
echo demo::a;
?>

Output:-

Example 3:-

<?php
abstract class Model
{
protected const TABLE_NAME = '';
public static function all()
{
return 'SELECT * FROM ' . static::TABLE_NAME;
}
}
class User extends Model
{
protected const TABLE_NAME = 'users';
}
class Role extends Model
{
protected const TABLE_NAME = 'roles';
}
echo User::all();
echo Role::all();
?>

Output:-

Conclusion

Use the const keyword to define a class constant. Use the visibility modifier keywords, including public, protected, and private. By default, a class constant is public. we have learnt many basic concept of Constant concept in PHP. Hope you have Enjoyed this. Thank you.

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.