Class constants in PHP
keyword used to declare a constant is ‘const’. Constants differ from normal variables in that you don’t use the ‘$’ symbol to declare or use them. The value must be a constant expression, and should not be a variable, a class member, result of a mathematical operation or a function call.
Example #1:
<?php
class MyClass
{
const constant = ‘constant value’;
function showConstant() {
echo self::constant . “\n”;
}
}
echo MyClass::constant . “\n”;
$classname = “MyClass”;
echo $classname::constant . “\n”; // As of PHP 5.3.0
$class = new MyClass();
$class->showConstant();
echo $class::constant.“\n”; // As of PHP 5.3.0
?>
Categories: OOPs in PHP, PHP5 Tags: class in PHP5, Oops in PHP, PHP5

.jpg)