PHP5

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
?>

Class constants

1 comment - What do you think?  Posted by admin - October 19, 2010 at 1:11 pm

Categories: OOPs in PHP, PHP5   Tags: , ,