website/errors/function.inner.md
<?php declare(strict_types = 1);
function outer(): void
{
function inner(): void
{
echo 'hello';
}
inner();
}
PHP allows declaring named functions inside other functions, but PHPStan does not support analysing them. Inner named functions in PHP have unusual scoping behavior -- they are registered in the global scope when the outer function is called, not when the inner function is declared. This makes them difficult to analyse statically.
Refactor the inner function to an anonymous function (closure):
<?php declare(strict_types = 1);
function outer(): void
{
- function inner(): void
- {
- echo 'hello';
- }
+ $inner = function (): void {
+ echo 'hello';
+ };
- inner();
+ $inner();
}
Or move the function to the top level:
<?php declare(strict_types = 1);
+function inner(): void
+{
+ echo 'hello';
+}
+
function outer(): void
{
- function inner(): void
- {
- echo 'hello';
- }
-
inner();
}