search

Home  >  Q&A  >  body text

I don’t quite understand the $query variable of “automatic global instantiation” in closure queries.

Why not use dependency injection in closures? For example,

function(\think\db\Query $q) {

$q->where('sex', 0)->where('salary', '> ', '8000');

}

tp5.1, I can execute it successfully like this.


土豆土豆2385 days ago1021

reply all(3)I'll reply

  • 亚连

    亚连2018-06-08 09:42:19

    This is an anonymous function, not a closure

    reply
    1
  • 土豆

    PHP manual, definition of anonymous function, The first sentence, "Anonymous functions, also known as closures, (Anonymous functions, also known as closures.)" Manual address: http://php.net/manual/en/functions.anonymous.php I know that the definition of closure in js is "a function that has access to variables in the scope of another function", but in php, closure seems to be the same thing as anonymous function.

    土豆 · 2018-06-08 13:22:45
    亚连

    Theoretically, closure and anonymity are different concepts, but PHP seems to combine them. Anonymity is closure, and closure is anonymity. You can read this article http://www.php.cn/php-weizijiaocheng-401974.html

    亚连 · 2018-06-08 15:36:17
  • Cancelreply