Const PHP Class (With Example Programs)

What is const PHP class?

The const PHP class is the function used to define or apply the constant as a class in programming.

This is usually applied when you need a class that should remain the same throughout the process.

In short, this PHP const works identically with the constants as variables.

This means that whether the constant is applied as a variable or as a class, its definition and application are still the same.

Moreover, if you need to define some constant data within a class, the default visibility of class constants (PHP const) is public, which can be handy.

However, class const is case-sensitive, so constants must be named entirely in capital letters.

Now, to access it from outside the class, we may also use the class name, the scope resolution operator (::), and then the name of the constant.

Example Program using Const Class in PHP

This time let us have some example programs where we can demonstrate the implementation of class const in PHP.

It will give a complete explanation of how the const in PHP class works and its proper arrangement.

Example Program 1: Simple Implementation

For the first example, we will have a very simple and plain line of code just to show how to define and use class const.

This will also provide a detailed explanation just right after the program output.

<?php
class Hi {
  const WELCOME = "Hi there ITSourceCoders!";
}
echo Hi::WELCOME;
?>

Output:

Hi there ITSourceCoders!

Code Explanation:

In our first example, we use the Hi as the program’s constant class.

The const WELCOME is the assigned constant within the class and has its finite value as "Hi there ITSourceCoders".

Now to test if the declaration of the PHP const class works, we use the echo function which will also display the output.

From the sample code, the program displays the output of Hi there ITSourceCoders!.

You can also explore the PHP class constants (const) with Namespaced that implements such as the example below:

Example Program 2: Defining and using a constant

<?php
class Abc
{
    const SAMPLE = 'Hello World!';

    function xyz() {
        echo  self::SAMPLE . "\n";
    }
}

echo Abc::SAMPLE . "<br/>";

$class1 = "Abc";
echo $class1::SAMPLE . "<br/>";

$class2 = new Abc();
echo $class2::SAMPLE."<br/>";
?>

Output:

Hello World!
Hello World!
Hello World!

Code Explanation:

This example program shows the proper implementation of PHP class const definition and usage.

Its class constant is the class Abc and the value of the constant within that class is the ‘Hello World!’.

Now to use the constant, we apply the function xyz enclosing the echo self::SAMPLE.

This will enable the programmers to call the function for their programs by calling the class, the function, or the constant directly.

You may also explore more about the PHP const class by implementing them into your programs now.

Additionally, the topic will give you enormous examples to help you understand the whole concept.

Example Program 3: Namespaced:: Class Example

<?php
namespace ITSC {
    class sample {
    }

    echo sample::class;
}
?>

Output:

ITSC\sample

Code Explanation:

As seen in this example, we can connect the value of the namespace with the class const in PHP.

This is possible by using the :: symbol to have the two values displayed at the same time.

You may use the symbol :: in various ways as you dig deeper into this tutorial.

PHP Class const Expression

Now, let us have an example to define the exact expression of PHP class const.

This will help you distinguish the proper way of using the constant class expressions.

<?php
const ONE = 1;
class foo {
    const TWO = ONE * 2;
    const THREE = ONE + self::TWO;
    const SENTENCE = 'The value of THREE is '.self::THREE;
}
?>

Code Explanation:

This example line of codes does not display any output since we haven’t used the echo function.

However, it is already clear as crystal by just looking into the program that the const ONE and its value is usable within the class foo.

The value of ONE is directly applied to the const TWO and for the other two arguments.

This also means that the program has to use another const to implement the exact value to pass to another.

PHP const Class and Inheritance

Now that you know the PHP const class, let us dive into its inheritance.

To know how this inheritance works, take a look at the example below.

Sample Code:

<?php

abstract class A
{
    protected const SAMPLE = '';

    public static function all()
    {
        return 'Get all data by ' . static::SAMPLE . "<br/>";
    }
}

class User extends A
{
    protected const SAMPLE = 'users';
}

class Role extends A
{
    protected const SAMPLE = 'roles';
}

echo User::all();
echo Role::all();
?>

This example shows a sample scenario where we apply the PHP const class and inheritance to let you understand how it works.

It will display the following output when we execute the program.

Output:

Get all data by users
Get all data by roles

Code Explanation:

In this example, we first define a constant named “SAMPLE” in the A class.

This returns a query that picks all rows from the SAMPLE constant-specified sample in the all() static method.

The next step is to define the user and role classes that extends the A class.

This activity will include defining the SAMPLE constant in the User and Role classes.

The all() function can be used by the user and role classes because they got it from the A class.

When the user class calls the all() function, the all() method returns the SAMPLE constant that the user class has set up and applies the same logic to the role class.

Summary

In summary, this topic has completed the whole discussion regarding const in the PHP class with example programs.

It is now clear as crystal that the concept of using constants in PHP as a class is applicable to maintain its value or implementation of it.

Moreover, it is also applicable in using constants in variables.

This is helpful in implementing variables in a PHP program that need to have or maintain their value throughout the process.

However, if you want any clarifications regarding this, you can immediately tap us in the comments below.

Leave a Comment