Wednesday, April 3, 2013

OOP -> Class Constants

Class constants are the constant(Never changed) declared in a class. constants are not start with ' sign and must be followed by "constant" keyword;

Example of constant
class MyClass  {
    const constant = '10';
    function showConstant() {
        echo  self::constant . "\n";
    }
}

//will print 10
echo MyClass::constant . "\n";


$obj1 = new MyClass();
//give error
echo $obj1->constant;


Rules for class constants
  • constant variable followed by constant
  • constant variables never start with $
  • constant variables can not be changed
  • constant variables can be used with scrope resolution (::) operator
  • constants variables cannot use with object

No comments:

Post a Comment