PHP OOPLesson 45Free preview
PHP Constructor
This lesson is part of PHP · All levels
Summary
Constructor is a special method called __construct that is run automatically by PHP once an object is created with new. Its primary function is to fill the initial value of the property so that the object is immediately ready for use. We can make the default parameter so that the constructor remains flexible and is called without an argument.
Key points
- Constructor is written as public in class.
- The constructor runs automatically when the object is created with new.
- The constructor is useful for initializing properties and opening up initial resources.
- The constructor parameter may have a default value so that the argument is optional.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.