The construct() Magic Method

Learn about the constructor method and how to write a constructor method without risking an error.

Constructor method

We use the __construct() method in order to do something as soon as we create an object out of a class. A method of this kind is called a constructor.

Note: The names of magic methods always start with two underscores. The __construct() magic method is no exception.

Usually, we use the constructor to set a value to a property. In our simple example, we should set the value of the $model property as soon as we create the object, so we add a constructor inside the class that sets the value of the $model property.

Get hands-on with 1400+ tech skills courses.