Lets say I have a closure:
$object->group(function() {
$object->add('example');
$object->add('example');
});
It won't work because $object is not defined in the closure.
Notice: Undefined variable: manager
So I would have to use ($object)
:
$object->group(function() use ($object) {
$object->add('example');
$object->add('example');
});
Now I want to keep it as simple as the first one so somehow $object has to be injected in to the closure.
The Laravel Framework does this with Routes for example:
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
I believe Laravel does this with the Reflection class.
How could I achieve this?