Call construction class

Asked

Viewed 566 times

1

Actually, this way, is it very 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());
    }

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

  • But I can work this way instead of instantiating ?

  • 1

    No, force typing or not, will not instantiate the object. only with the new same.

  • But I can access the methods of this class !

  • Are we talking about the class that owns the constructor or the parameter class? Because by forcing the typing makes your IDE start to see what’s in that variable because he’s sure what her class is going to be, so he makes the methods available to you.

  • If you are talking about the $this->class, you have to instantiate the object before passing the parameter.

  • If I fuzer like this it works $this->class->method()

Show 2 more comments

3 answers

2

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

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

The drawback is that if you for some reason want to pass another type of data you will need to rewrite the method. But this drawback is actually a poor structuring of the code because that’s the best way to work and write clean, clear code.

1

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

See what your statement 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 by parameter in the constructor.

Behold:

 new X(new Classe);

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

new X(1);

Exit:

X::__Construct() must be an instance of Classe, integer Given.

0

For that class Financeirocontroller is instantiated, it needs to receive 4 parameters in its constructor, the type that is induced is the name of the classes you need to use, one of them is repository type and the other three services type. Usually these classes are branched within your project /entity/entities/ and /service/services/.

To better understand the concept, I suggest you try to study Design Patterns and Strategy Patterns.

If you are already used to English, here is a very good site: https://sourcemaking.com/design_patterns/strategy

If you prefer something in Portuguese, have this site also that is excellent: http://br.phptherightway.com/pages/Design-Patterns.html

To understand this type of induction, the PHP documentation has more information: http://php.net/manual/en/language.oop5.typehinting.php

  • This I understand, what I do not understand is that if I can access the methods of the class typing it this way so that instantiate ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.