Why can't constants be accessed outside the class definition in PHP?

Constants in PHP are scoped to the class in which they are defined. This means that they cannot be accessed outside the class definition directly. To access a constant from outside the class, you can create a public static method in the class that returns the constant value.

class MyClass {
    const MY_CONSTANT = 'Hello World';

    public static function getConstant() {
        return self::MY_CONSTANT;
    }
}

echo MyClass::getConstant(); // Output: Hello World