PHP OOP – Class Constants

In PHP OOP, class constants are values that are defined at the class level and remain constant throughout the execution of the script. These constants are declared using the const keyword, followed by the name of the constant and its value.

Here is an example of a class constant in PHP:

class MyClass {
    const PI = 3.14;
}

In this example, PI is a class constant with a value of 3.14.

Class constants can be accessed using the :: operator, like this:

echo MyClass::PI;

The output of this code would be 3.14.

Class constants are similar to static properties in that they can be accessed without creating an instance of the class. However, class constants cannot be changed once they are defined, while static properties can be changed during the execution of the script.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial