i

PHP Tutorial

PHP-Constants

PHP class constants are like normal constant that cannot be changed after declaration. Whenever you want to define some constant data in a class, you can do it using class constants. To use class constants, you need const keyword and the constants are case sensitive. If you want to access the constant outside a class you can use class name with (::) scope resolution operator and constant name .

For example:

class Wishes{

const LAST_GREETING = “Thank you for Visiting our site!”; }

echo Wishes:: LAST_GREETING;

?>

If you want to access the constant within a class using method, you can use self keyword with (::) and constant name. For example:

class Wishes {

const LAST_GREETING = “ Thank you for Visiting our site!”;

public function greeting() {

echo self :: LAST_GREETING;

}

}

$visit_end = new Wishes();

$visit_end = greeting();

?>