PHP – Class Constants

Class constants can be useful if you need to define some constant data within a class. A class constant is declared inside a class with the const keyword. A constant cannot be changed once it is declared.

<?php
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com!";
}
echo Goodbye::LEAVING_MESSAGE;
?>
<?php
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com 2!";
public function byebye() {
echo self::LEAVING_MESSAGE;
}
}
$goodbye = new Goodbye();
$goodbye->byebye();
?>

Leave a Reply