Two __Construct() Functions in the Same Project

Asked

Viewed 347 times

4

I can’t have two functions __Construct() in my project Laravel ?

There is one in Controller.php and wanted to build one __Construct in another Controller.

Just give me variable errors on the pages...

I’m wearing a middleware in the __Construct and want to use in a controller.

I tried to use in a function that opens the page but does not let. Just let use in __Construct.

1 answer

5


Wouldn’t it just be doing this?

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

From what 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 Controller, but cannot lose the definitions of Controller.

In the PHP, parent means you are accessing the Father 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, the other methods can be overwritten, but have to have the same signature as the original method (the same parameters). But the method __construct does not have this restriction. So, if you want to overwrite the __construct adding new parameters, no problems.

  • If it’s not clear, why did you answer ? It worked. That’s right. Thanks. I didn’t want to leave everything I need in the same.

  • 2

    @Peterparker wasn’t clear, but I guessed that was it. I empathize with other PHP programmers' problems..

  • 2

    And it doesn’t: I know you’re Zoom :D

Browser other questions tagged

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