Menu

← Back to magicConstant.*

Error Identifier: magicConstant.outOfFunction

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 __FUNCTION__;
echo __METHOD__;

Why is it reported? #

The magic constants __FUNCTION__ and __METHOD__ are used outside of a function or method. In this context, they always resolve to an empty string '', which is almost certainly not the intended behavior.

PHP defines these magic constants to return the name of the current function or method. When used at the top level of a script or in a class property initializer (outside a method), there is no function context, so the value is always empty.

How to fix it #

Move the usage inside a function or method where the magic constant has a meaningful value:

-echo __FUNCTION__;

+function myFunction(): void
+{
+    echo __FUNCTION__; // outputs "myFunction"
+}

If you need the file or line information at the top level, use an appropriate magic constant instead:

-echo __FUNCTION__;
+echo __FILE__;

How to ignore this error #

You can use the identifier magicConstant.outOfFunction to ignore this error using a comment:

// @phpstan-ignore magicConstant.outOfFunction
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: magicConstant.outOfFunction

Rules that report this error #

  • PHPStan\Rules\Constants\MagicConstantContextRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.