4
I believe that this is a fully valid curiosity, since it greatly facilitates and speeds up development.
I always analyze the code of the frameworks I use, such as Cakephp 2
, Laravel 4
and Symfony 2
.
I just started working with Laravel 5
and I realized that in it there’s a feature that I’ve never seen in any of the frameworks. It’s the automatic passing of an instance to the argument of a method of a controller or route, or any other class of the framework, simply by defining the Type Hiting
of function.
Example:
class AuthController extends Controller
{
public function getIndex(Request $request, UrlGenerator $url)
{
$request->get('nome');
}
}
By doing this in the parameters of getIndex
, automatically, instances of classes are passed to the method. So, if I don’t want to use UrlGenerator
, I can simply remove the parameter declaration from that method, which it will not be passed by argument.
I mean, no matter the position of the parameters, it always gives me the instance of the typed argument simply because I passed it there.
Another example:
public function getIndex($id, Request $request){}
Or
public function getIndex(Request $request, $id) {}
How is it possible to do this in php? How Laravel
does that?
Please avoid long discussions in the comments; your talk was moved to the chat
– Maniero