1

I created a model in my laravel project and put constructor function in there however someone told me there is a prebuilt constructor method in laravel's eloquent.

Does it mean that creating my __constructor() I am overriding the present constructor that is hidden somewhere?

divHelper11
  • 2,090
  • 3
  • 22
  • 37

1 Answers1

0

You can always call the parent constructor inside your constructor:

class MyModel extends Eloquent {

     function __construct() {
           parent::__construct();
    }   
}

edit: this will call the following constructor for you https://github.com/illuminate/database/blob/v4.2.17/Eloquent/Model.php#L243

Sometimes you want to call your own code before the parent constructor call and sometimes after, I hope that this link helps determine which of 2 situations best suits your needs!

online Thomas
  • 8,864
  • 6
  • 44
  • 85
  • Thank you! But thi parent construct is always used automatically while creating an object without calling it by myself right? – divHelper11 Feb 06 '17 at 12:15
  • 1
    Normally yes, but you are overwriting it as soon as you are writing your own constructor, that's why. Same goes for other functions with exactly the same name. http://stackoverflow.com/questions/2994758/what-is-function-overloading-and-overriding-in-php – online Thomas Feb 06 '17 at 12:19