HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Constants cannot be changed once it is declared
Constants cannot be changed once it is declared.
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.
Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.
We can access a constant from outside the class by using the class name
followed by the scope resolution operator (::
) followed by the constant
name, like here:
<?php
class
Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3C!";
}
echo
Goodbye::LEAVING_MESSAGE;
?>
Or, we can access a constant from inside the class by using the
self
keyword followed by the scope resolution operator (::
) followed by the constant name, like here:
<?php
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3C!";
public function byebye() {
echo self::LEAVING_MESSAGE;
}
}
$goodbye = new Goodbye();
$goodbye->byebye();
?>