Two Functions __construct () in Same Project

4

I can not have two construct () functions in my Laravel project?

You have one in the Controller.php and wanted to mount a __ construct on another Controller.

It only gives me variable errors on the pages ...

I'm using a middleware in __ construct and want to use it in a controller .

I tried to use in a function that opens the page, but it does not. Just let it use in __ construct .

    
asked by anonymous 16.05.2016 / 15:36

1 answer

5

Would not it be just doing just that?

class PaginaController extends Controller
{
       public function __construct()
       {
            // Faz alguma coisa diferente
            parent::__construct();
       }
}

As I understand your question, you want to have something defined in the constructor of the Controller class, but you want to add a new functionality in the constructor of a child controller of Controller , but you can not lose the settings of Controller .

No PHP , parent means that you are accessing the parent class method.

Example:

 class Pato {

         public function podeVoar() { return true; }

         public function podeAndar() { return true; }
 }

 class Patinho extends Pato {

     public $idade = 1;

     public function podeVoar() {
          if ($this->idade > 2) {
               return parent::podeVoar();
          }

          return false;
     }
 }

Note : Remember that in PHP, other methods can be overwritten, but must have the same signature as the original method (the same parameters). However, the __construct method does not have this restriction. So if you want to overwrite __construct by adding new parameters, there are no problems.

    
16.05.2016 / 15:43