How can constants be defined in a class in PHP?
To define constants in a class in PHP, you can use the `const` keyword followed by the constant name and its value. Constants in a class are defined using the `const` keyword inside the class definition and can be accessed using the `::` scope resolution operator. Constants are useful for defining values that should not change throughout the execution of the program.
class MyClass {
const MY_CONSTANT = 10;
public function getConstantValue() {
return self::MY_CONSTANT;
}
}
$myObject = new MyClass();
echo $myObject->getConstantValue(); // Output: 10