Benefits of using a constructor?

前端 未结 5 1535
眼角桃花
眼角桃花 2020-12-10 07:49

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,

5条回答
  •  甜味超标
    2020-12-10 08:06

    The constructor is for initialisation done when an object is created.

    You would not want to call an arbitrary method on a newly created object because this goes against the idea of encapsulation, and would require code using this object to have inherent knowledge of its inner workings (and requires more effort).

提交回复
热议问题