F
F
fuckman2015-10-13 15:24:30
Laravel
fuckman, 2015-10-13 15:24:30

Why in php pass the class name of the argument to the method?

I am new to php, learning laravel. There, class names are often passed to methods, why is this done and what is the name of this feature?
For example:

public function __construct(Guard $auth, Registrar $registrar)
  {
    $this->auth = $auth;
    $this->registrar = $registrar;

    $this->middleware('guest', ['except' => 'getLogout']);
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Radiks Alijevs, 2015-10-13
@fuckman

This is called Dependency Injection, in parentheses you pass the object that you will use inside the class.
If the object is passed through __construct(Guard $auth), as in your code, then you can use this class in every method. If you only need to use the class once, you can pass the desired class to the method instead of
Passing the second parameter can be like this:
or like this:
Laravel knows what is where

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question