In PHP an unchangeable or constant value can be created by using a basic PHP class. That constant will be different than a normal variable because it does not initialize with “$” sign.
Initialization of a class constant is described below.
Syntax for a class constant
<?php
// creating class with class constant
class classConstant
{
const CONS = "value here...";
}
// accessing constant
echo classConstant::CONS;
?>
After a class is defined, it may be used where ever needed in the code. To access a class and use it's constants, properties and methods a prototype has to be created that refers to that class. That is done with a keyword new
. To access methods a new class prototype must use ->
sign. The example below shows how to properly do it:
Example of creating and using a class constant
Comments
No comments have been made yet.
Please login to leave a comment. Login now