Please wait

Constructor Method

Classes can have something called magic methods. Magic methods in PHP are special predefined methods that have specific names starting with two underscores (__). These methods are triggered automatically in response to certain actions. In other words, they're "magical" because you don't explicitly call them; PHP does it for you when a specific condition is met.

Think of a magic method like an automatic door at a grocery store. When you approach the door (or meet a condition), it opens automatically (or the method is called) without you needing to do anything.

The construct method

For example, one of the most commonly used magic methods is the __construct() method. This method is automatically called when you create a new object from a class. It's often used to set initial values for object properties.

Here's an example:

class Dog {
  public $name;
  // This is the magic constructor method
  public function __construct($dogName) {
    $this->name = $dogName;
$rex = new Dog('Rex'); // The __construct method is automatically called here
echo $rex->name; // Outputs 'Rex'

In this example, when we instantiate the Dog object ($rex), PHP automatically calls the __construct() method and passes 'Rex' as the argument. This sets the $name property of the new object.

Avoid using underscores

Almost all magic methods in PHP always start with two underscore characters (__). For this reason, you should avoid using the same naming convention for custom methods even though you're allowed to. This practice allows you to avoid the possibility of conflicting with PHP's magic methods.

Constructor Promotion

Constructor property promotion is a feature added in PHP 8.0 that provides a shorthand syntax to define class properties and a constructor in a single declaration.

Before this feature, if you wanted to define a class with properties, and those properties were to be set via the __constructor(), you would have to do something like this:

class Dog {
  public $name;
  public $age;
  public function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
$rex = new Dog('Rex', 5);

Here, we had to manually declare the properties $name and $age, then we had to define a constructor function that takes $name and $age as arguments, and finally, we had to assign those arguments to the corresponding properties using $this->.

With constructor property promotion, this boilerplate code can be greatly reduced. We can define properties directly in the constructor like this:

class Dog {
  public function __construct(public $name, public $age) {
    // nothing else needed here
$rex = new Dog('Rex', 5);

In this case, PHP automatically creates the $name and $age properties for us and assigns the passed values to them. This makes our class definition shorter and cleaner.

So, to sum up, constructor property promotion is a shorthand way of defining class properties and a constructor at the same time, making your code more compact and readable. It's one of the great new features added in PHP 8.0.

Key takeaways

  • Magic methods in PHP are special methods that start with two underscores (__) and are triggered automatically when certain conditions are met.
  • The __construct() method is a magic method that's automatically invoked when an object is created from a class.
  • It's commonly used to initialize object properties or to perform actions needed for object setup.
  • The __construct() method can take parameters, allowing you to pass values when creating a new object.
  • Constructor property promotion is a feature added in PHP 8.0 that simplifies the creation of classes.
  • It allows you to define class properties directly in the constructor, reducing boilerplate code.


Please read this before commenting