Share on Google+Share on Google+

PHP Class Constant

Advertisement
In this tutorial we will study how to declare a class constant in PHP. Sometimes we need to have constant values in our program which remains same through and through. We do not need to put a '$' sign before the constant to use or to declare it.

PHP Class Constants:

Sometimes we need to have constant values  in our program which remains same through and through. We do not need to put a $ sign before the constant to use or to declare it.

The value of the constant should remain unchanged that means it must be a constant expression.

 

Example:

<?php

class A

{

const newValue="Constant value does not consist $ sign";

function display()

{

echo self::newValue."\n";

}

}

$a=new A();

$a->display();

?>

 

Output:

Constant value does not consist $ sign

Explanation:
In the above example there is a single class declaration named A. Outside of this class

Advertisements

Posted on: February 18, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.