Courses Job Ready Fresher Training AI for Class 7 to 12 Corporate Training Placements Tutorials
Free Learning Resources

IT Tutorials & Interview Prep

Free guides, interview Q&As, and job responsibility breakdowns — curated by industry veterans to help you crack MNC interviews

109+
Tutorial Articles
9
Topic Categories
100%
Free to Read
← Back to PHP

Class Constants

PHP Last Updated: Oct 17, 2025

Class Constants

Constants are like variables except that once they are defined, they cannot be changed. If we need to define some constant data within a class, we need to use class constants. Class constants are case-sensitive and they can be declared inside a class with the const keyword. We can also access a constant outside of a class by using the class name followed by the :: operator with the constant name.

Example:

<?php
class Namaste {
 const GREETING_MSG = "Namaskaram Dosto!!";
}

echo Namaste::GREETING_MSG;
?>

We can also use the self keyword to access the constant inside the class.

Example:

<?php
class HelloWorld {
 const NEW_MESSAGE = "Did I leave the Stove on?";
 public function ByeWorld() {
   echo self::NEW_MESSAGE;
 }
}

$helloworld = new HelloWorld();
$helloworld->ByeWorld();
?>

Want to practice these scenarios in a real IT environment? Our 45-Day Internship Program gives you hands-on experience with Active Directory, Ticketing Tools, and Networking.
Join the Internship →