0

Running PHP CodeSniffer on the below snippet:

return array_filter($methods, function ($method) {
    return in_array(
        strtolower($method['type']),
        self::$types
    );
});

I get this error message:

Use of self::$types inside closure.

What exactly is the error reported (why I can't use self::$types inside the closure) and how to solve it?

Note: the code is perfectly working anyway. The reported error is from CodeSniffer (not a PHP error).

Andrea
  • 15,900
  • 18
  • 65
  • 84
  • If you use the `-s` command line argument for PHPCS, it will tell you what sniff generated the error, and from what coding standard. Maybe you can find more information once you know that code, or find out who to ask about it. – Greg Sherwood Jan 13 '18 at 02:42

1 Answers1

0
return array_filter($methods, function ($method) use (self::$types) {
    return in_array(
        strtolower($method['type']),
        self::$types
    );
});

Read about inheriting from parent scope here: http://php.net/manual/en/functions.anonymous.php

Emile Pels
  • 3,837
  • 1
  • 15
  • 23
  • Can you elaborate a bit your answer, please :).. Why I can't use `self::$types` inside a closure and why adding `use (self::$types)` should solve the error? – Andrea Jan 12 '18 at 09:14