How does the initialization of static variables work in PHP?

When initializing static variables in PHP, it's important to ensure that the initialization is done correctly to avoid unexpected behavior. One common mistake is trying to initialize a static variable with a non-constant expression, which can lead to parse errors. To initialize static variables properly, use a constant value or assign the value inside a static method.

class MyClass {
    public static $staticVar;

    public static function initializeStaticVar() {
        self::$staticVar = 'initialized';
    }
}

MyClass::initializeStaticVar();
echo MyClass::$staticVar; // Output: initialized