Call class constructor

1

Actually, this way, is it really wrong? I'm not really understanding the typing:

class FinanceiroController extends Controller
{

    /**
     * @var ChamadosFinanceirosRepository
     */
    private $chamadosFinanceirosRepository;
    /**
     * @var FinanceiroService
     */
    private $financeiroService;
    /**
     * @var ChamadosFinanceirosService
     */
    private $chamadosFinanceirosService;
    /**
     * @var ChamadosParcelasPagasService
     */
    private $chamadosParcelasPagasService;

    public function __construct(ChamadosFinanceirosRepository $chamadosFinanceirosRepository, FinanceiroService $financeiroService,
                                ChamadosFinanceirosService $chamadosFinanceirosService, ChamadosParcelasPagasService $chamadosParcelasPagasService)
    {
        $this->chamadosFinanceirosRepository = $chamadosFinanceirosRepository;
        $this->financeiroService = $financeiroService;
        $this->chamadosFinanceirosService = $chamadosFinanceirosService;
        $this->chamadosParcelasPagasService = $chamadosParcelasPagasService;
    }
    public function getProvisionamentoPrestador()
    {
        return view('financeiro.provisionamentoPrestador');
    }
    /**
     * @return mixed
     * Busca os chamados financeiros com status_provisionamento_sac finalizado
     */
    public function getBuscaPrestadoresFinanceirosSac()
    {
        return $this->chamadosFinanceirosService->buscaProvisionamentosSac(['status_provisionamento_sac','finalizado']);
    }
    /**
     * @return mixed
     * Busca os chamados financeiros pelo id financeiro
     */
    public function getBuscaPrestadoresFinanceirosSacId()
    {
        //return $this->chamadosFinanceirosService->buscaProvisionamentosSac(['id',\Request::input('id')]);
        return $this->financeiroService->buscaProvisionamentosSac(['id',\Request::input('id')]);
    }

    public function postGravaPagamentoPrestadorChamado()
    {
        return $this->chamadosParcelasPagasService->pagamentoPrestadorChamado(\Request::all());
    }

}
    
asked by anonymous 25.08.2015 / 17:26

3 answers

2

In this case you are forcing the typing of the parameter for a class of type "Class".

The advantage is that you guarantee the type of data and can work better with the polymorphism and ensure the integrity of the system.

The downside is that if you for some reason want to pass on another type of data you will need to re-write the method. But this drawback is actually a poor structuring of the code as this is the best way to work and write clean and clear code.

    
25.08.2015 / 17:30
1

You are not calling the class in the constructor. You are inducing the type that will be accepted by parameter.

See what your claim is for:

class X {

   private $classe;

    public  function __construct(Classe $classe)
    {
           $this->classe = $classe;
    }
}

In this case, you are saying that only the instance of Classe will be accepted per parameter in the constructor.

See:

 new X(new Classe);

If you try to pass any other value, this will generate an error:

new X(1);

Output:

  

X :: __ construct () must be an instance of Class, integer given.

    
25.08.2015 / 17:29
0

In order for the FinancialController class to be instantiated, it must receive 4 parameters in its constructor, the type that is induced is the name of the classes you need to use, one of which is repository type and the other three of the type of services. Generally these classes are branched into your project /entity/entities/ and /service/services/ .

To better understand the concept, I suggest you look at Design Patterns and Strategy Patterns .

If you are already used to English, here is a very good website: link

If you prefer something in Portuguese, this site is also excellent: link

To understand this type of induction, the PHP documentation has more information: link

    
25.08.2015 / 22:40