如何将函数作为参数传递给php类

时间:2019-10-01 11:27:02

标签: php

我想编写一个使用function方法提交的类,并在该类中访问example_method

$users = User::where('posts', function($q){
    $q->example_method ('created_at', '>=', '2015-01-01 00:00:00');
})->get() ;

1 个答案:

答案 0 :(得分:1)

据我了解-您想访问匿名函数中的$q变量,可以通过以下方式访问它:

$users = User::where('posts', function() use ($q) {
    $q->example_method ('created_at', '>=', '2015-01-01 00:00:00');
})->get() ;

如果要访问在函数外部定义的变量,则必须使用use继承它。

更多信息:https://www.php.net/manual/en/functions.anonymous.php