Class constant in PHP OOP | PHP OOP Lecture 6 | How to do this

Опубликовано: 23 Октябрь 2024
на канале: arbn Studios
130
10

Class constant in PHP OOP, Constant in PHP OOP
#ClassConstantInPhpOOP,#ConstantInPhpOOP,#HowToDoThis


For further discussion please join our Facebook group:-
  / 1184520091737540  

For further updates please like our Facebook Page:-
  / web-learners-100953708317292  



PHP - Class Constants
Constants cannot be changed once it is declared.
Class constants can be useful if you need to define some constant data within a class.
A class constant is declared inside a class with the const keyword.
Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.

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.


Or, 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.


It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that you don't use the $ symbol to declare or use them. The default visibility of class constants is public.

The value must be a constant expression, not (for example) a variable, a property, or a function call.
The special ::class constant is available as of PHP 5.5.0, and allows for fully qualified class name resolution at compile time, this is useful for namespaced classes.

Constants are one type of variable which we can define for any class with keyword const.
Value of these variables cannot be changed any how after assigning.
Class constants are different than normal variables, as we do not need $ to declare the class constants.
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.