Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of using a constructor?

In my quest in trying to learn more about OOP in PHP. I have come across the constructor function a good few times and simply can't ignore it anymore. In my understanding, the constructor is called upon the moment I create an object, is this correct?

But why would I need to create this constructor if I can use "normal" functions or methods as their called?

cheers, Keith

like image 878
Keith Donegan Avatar asked Dec 31 '08 18:12

Keith Donegan


1 Answers

The constructor allows you to ensure that the object is put in a particular state before you attempt to use it. For example, if your object has certain properties that are required for it to be used, you could initialize them in the constructor. Also, constructors allow a efficient way to initialize objects.

like image 200
Brian Fisher Avatar answered Oct 23 '22 11:10

Brian Fisher