Menu

← Back to outOfClass.*

Error Identifier: outOfClass.self

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

echo self::FOO; // ERROR: Using self outside of class scope.

Why is it reported? #

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.

How to fix it #

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
	}
}

How to ignore this error #

You can use the identifier outOfClass.self to ignore this error using a comment:

// @phpstan-ignore outOfClass.self
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: outOfClass.self

Rules that report this error #

  • PHPStan\Rules\Classes\ClassConstantRule [1]
  • PHPStan\Rules\Classes\ExistingClassInInstanceOfRule [1]
  • PHPStan\Rules\Classes\InstantiationRule [1]
  • PHPStan\Rules\Methods\CallStaticMethodsRule [1]
  • PHPStan\Rules\Methods\StaticMethodCallableRule [1]
  • PHPStan\Rules\Properties\AccessStaticPropertiesInAssignRule [1]
  • PHPStan\Rules\Properties\AccessStaticPropertiesRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.