How can the error "Using $this when not in object context" be avoided when working with static methods in PHP?

When working with static methods in PHP, the error "Using $this when not in object context" occurs when trying to access non-static properties or methods using $this within a static method. To avoid this error, you should use the self keyword to reference static properties and methods within the class.

class MyClass {
    private static $myStaticProperty = 'Hello';

    public static function myStaticMethod() {
        return self::$myStaticProperty;
    }
}

echo MyClass::myStaticMethod(); // Output: Hello