Back to Developer Roadmap

Magic methods

src/data/roadmaps/php/content/[email protected]

4.0965 B
Original Source

Magic methods

PHP Magic Methods, often considered the hooks of the language, provide developers a way to change how objects will respond to particular language constructs. Magic methods are special functions that start with "__" such as __construct(), __destruct(), __call(), __get(), __set() and more. They enable us to perform certain tasks automatically when specific actions occur. For example, __construct() executes when an object is created while __destruct() triggers when an object is no longer needed. Let's see the __construct magic method in action:

class Car {
    public $color;
    public function __construct($color) {
        $this->color = $color;
    }
}
$blueCar = new Car("Blue"); // This will call the __construct() method.
echo $blueCar->color;  // Outputs "Blue".

Visit the following resources to learn more: