CLASS CONSTANTS in PHP5
- We can define the constants inside class.
- Constants are belong to class as like static members. They are not belong to instance of the class.
- We can access constants as similar to accessing static members:
- The value of constants are neither changed nor removed after they are defined
Example:
class MyClass {
const VAR1 = "one";
function printConstant()
{
echo self::VAR1;
}
}
echo MyClass::VAR1;
$obj = new MyClass();
$obj->printConstant();
In the above example this line will print “one”
echo MyClass::VAR1;
And also inside the class you can use the contants like
self::VAR1;
