Magic Methods in PHP

What are PHP Magic Methods?

PHP Magic methods are a specialized method that is executed in response to a specific event. Each magic methods start with two underscores(__) and it is called automatically when a certain condition is met. It triggers custom behaviors defined by a programmer inside the class. Some of the magic methods are __construct(), __destruct(),  __toString(),  __get(),  __set(), __call() etc.

In this tutorial, we are going to explore php magic methods.

PHP Magic Methods

PHP Magic Methods

Constructors and Destructors

Constructors (__construct) is the most common magic methods. Constructors and Destructors are called when an object is created and destroyed.

__construct

The __construct() method is called when an object of a class is created. It is the first method which is called when an object of a class is created.

A Constructor is used for any initialization that the object may need before it is used.

In above example, we are creating an instance of the Student class and passing a parameter(value of age) that will be injected into the __construct() method. We don’t have to call __construct() method it will be automatically called when an instance of a student class is created.

How to call a parent constructor in PHP

Singleton design pattern

__destruct

The __destruct() method is called when an object of a class is destroyed.

The __destruct() method will be useful for any cleanup operations after an object is destroyed. Like __construct() method it’s also called automatically. The lifecycle of a PHP request is very short, so it is rare to find the use of a __destruct() method in our codebase.

__get() Magic Method

The __get() method is called when the object of a class try to read a property that is inaccessible or unavailable.

Inaccessible means either the property is not defined or it is not public.

In the above code, we are trying to access private property outside the class which leads to a fatal error. Let’s modify the code and use __get() method.

PHP built-in web server

Take another example, In this code, we are trying to echo a property which doesn’t exist.

__set() Magic Method

The __set() method is called when code tries to set a method that is not accessible or doesn’t exist.

The __get() and __set() method is not a substitutes for getters and setters. They just allow you to handle method class or property access that would otherwise result in an error. As compared to proper getter and setters they are considerably slower.

__isset() Magic Method

The __isset() method is called when isset() or empty() check non-existent or inaccessible class property.

NOTE – isset() is a language construct whereas __isset() is a magic method.

Difference between isset() and __isset()

Difference between isset() and __isset()

Difference between isset() and __isset()

__call() Magic Method

The __call() method is called when the method of a class does not exist or inaccessible.

__callStatic() Magic Method (Available as of version PHP 5.3)

The __callStatic() method is similar to __call method except it is called when code attempts to call a static method which doesn’t exist or inaccessible.

Singleton design pattern

__toString() Magic Method

The __toString() method is called when a code tries to treat an object like a string. This method doesn’t accept any argument.

Abstract class vs interface in PHP

__clone() Magic Method

In PHP, Objects are passed by reference. It means if we copy an object, then it will be copied by reference, not by value.

Difference between call by value & call by reference in PHP

To understand this let’s take an example –

In order to create a copy of an object, we need to use clone keyword.

The __clone() magic method is used to change the behaviour of a clone object.

Cons of Magic Method

1. Magic methods are slow.

2. Magic methods are public so it ignores scope.

Tagged , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.