🐘 PHP Beginner

What is the __construct() method in PHP?

Why Interviewers Ask This

This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex PHP topics. It also reveals how well you can explain technical ideas to non-experts.

Answer

The __construct() method is PHP's constructor — it is automatically called when an object is created with new. It is used to initialize object properties and perform setup. PHP 8.0 introduced constructor property promotion, which dramatically reduces boilerplate: class User { public function __construct(private string $name, private int $age) {} } — declaring the parameter with an access modifier automatically creates and assigns the property. PHP only supports one constructor per class (no overloading), but you can use default parameter values to simulate multiple constructor signatures. The counterpart is __destruct(), called when the object is destroyed.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.