website/errors/outOfClass.self.md
<?php declare(strict_types = 1);
echo self::FOO; // ERROR: Using self outside of class scope.
The keyword self refers to the class in which it is used. It can only be used inside a class definition (in methods, class constants, or property declarations). Using self outside of a class scope (for example, in a function or in the global scope) is a PHP error because there is no class to reference.
Use the fully qualified class name instead of self when outside a class:
<?php declare(strict_types = 1);
-echo self::FOO;
+echo MyClass::FOO;
Or move the code inside a class method:
<?php declare(strict_types = 1);
class MyClass
{
public const FOO = 'bar';
public function doFoo(): void
{
echo self::FOO; // This works inside a class
}
}