Or, we can access a constant from inside the class by using the selfkeyword followed by the scope resolution operator (::) followed by the constant name, like here:
Example
<?php
classCar {
const WHEELS = 4; // Defining a constant
publicfunctiongetWheels() {
return"This car has " . self::WHEELS . " wheels.";
}
}
// Accessing the constant using the class name
echo"A car has " . Car::WHEELS . " wheels.<br>";
// Accessing the constant using `self::` inside the class
We use cookies and similar technologies to enhance your browsing experience, serve personalized content and advertisements, and analyze our traffic.
By clicking "Accept All", you consent to our use of cookies and the processing of your personal data for these purposes.
You can manage your preferences or learn more in our
Privacy Policy and
Cookie Policy.