CLASS CONSTANTS in PHP5

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;

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.