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?
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,
Which are the points we have to know about the Constants?
- 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?
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:-
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:-
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!
Example 1:-
Output:-
Example 2:-
Output:-
Example 3:-
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.
- Discover 7 Fascinating Careers in Game Design - October 14, 2024
- The Integration of AI and IoT: Enhancing Smart Systems - October 8, 2024
- Software Development Companies in Latin America and How To Choose One - October 1, 2024