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

如何将函数作为参数传递给php类,php,Php,我想用function方法编写一个提交类,并在类中访问example_方法 $users = User::where('posts', function($q){ $q->example_method ('created_at', '>=', '2015-01-01 00:00:00'); })->get() ; 据我所知-您希望在匿名函数中访问$q变量,可以通过以下方式访问: $users = User::where('posts', function() use

我想用function方法编写一个提交类,并在类中访问example_方法

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

据我所知-您希望在匿名函数中访问
$q
变量,可以通过以下方式访问:

$users = User::where('posts', function() use ($q) {
    $q->example_method ('created_at', '>=', '2015-01-01 00:00:00');
})->get() ;
如果您想访问在函数外部定义的变量,您必须使用
use
继承它

更多信息: