PHP Constant in Oops

PHP Constant in Oops


Constants are bits of data saved in the computer’s memory that can not be modified once they have been assigned.

Constants declared within classes are known as class constants.A class constant is declared with the const keyword. Class constant must be declared inside class.Class constants are case-sensitive. It is preferred that the constants be named entirely in uppercase characters.

Constants can be accessed in two ways.

  1. Accessing Constant Inside Class
  2. Accessing Constant Outside Class

here is the Exapmple given below Accessing Constant Inside and Outside the class.

for Constant Inside class: we can access a constant from inside the class by using the self keyword followed by the scope resolution operator (::) followed by the constant name, like here:

Output :Marvel

For Constant Outside the class :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, like here:

Output : Web Designing house