# PHP Magic Methods Cheatsheet

Difficulty:BeginnerLength:MediumLanguages:

In this post, I'll give you a cheatsheet quick reference to all the magic methods available in PHP.

Whether you are a seasoned PHP developer or a novice in PHP development, if you’ve worked with OOP in PHP, then you’re using at least a couple of PHP magic methods already. However, if you have not heard of PHP magic methods yet, let me introduce them:

The function names __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo() are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them. All magic methods MUST be declared as public.

If you’re wondering why these methods are called magic methods, it’s because of the fact that if you define one of these methods in your class, it’ll be called automatically, and you are just supposed to define what should happen within it. The best example of a magic method is the __construct() function, which is called automatically when the object is instantiated.

In general, magic methods in PHP allow you to perform various kinds of operations with objects. They also allow you to handle certain kinds of situations by manipulating objects.

## PHP Magic Methods

### The __construct() Method

If you define this method in your class, it’ll be called automatically when an object is instantiated. The purpose of this method is to assign some default values to object properties. This method is also called a constructor.

Let’s have a look at a quick example to understand how it works:

In the above example, when you instantiate a new object with new student('John','john@tutsplus.com'), it calls the __construct() method in the first place. In the __construct() method, we’ve assigned values passed in the arguments to the object properties.

### The __destruct() Method

The __destruct() method is called a destructor, and it’s called when the object is destroyed. Generally, it’s also called when the script is stopped or exited. The purpose of this method is to provide an opportunity to save the object state or any other cleanups you would want to perform.

Let’s have a look at the following example:

### The __set() Method

The __set() magic method is called when you try to set data to inaccessible or non-existent object properties. The purpose of this method is to set extra object data for which you haven’t defined object properties explicitly.

Let’s get back to our example to understand how it works.

As you can see in the above example, we’re trying to set the phone property, which is non-existent. And thus, the __set() method is called. The first argument of the __set() method is the name of the property which is being accessed, and the second argument is the value we’re trying to set.

### The __get() Method

In the case of the __set() method example in the previous section, we discussed how to set values for non-existent properties. The __get() method is exactly the opposite of it. The __get() magic method is called when you try to read data from inaccessible or non-existent object properties. The purpose of this method is to provide values to such properties.

Let’s see how it works in action.

### The __toString() Method

The __toString() magic method allows you to define what you would like to display when an object of the class is treated like a string. If you use echo or print on your object, and you haven’t defined the __toString() method, it’ll give an error.

Let’s try to understand it with the following example.

### The __clone() Method

If you want to duplicate an existing object, you could use the clone keyword to do that. But after cloning, if you want to modify properties of the cloned object, you can define the __clone() magic method in your class.

The issue with the above approach is that it makes a shallow copy of the object while cloning, and thus internal objects of the cloned object will not be cloned.

In the context of the above example, if you hadn’t defined the __clone() method, the cloned object, $objStudentTwo, would still point to the same Student_School object referenced by the $objStudentOne object. Thus, by implementing the __clone() method, we make sure that the Student_School object is cloned along with the main object.

### The __debugInfo() Method

The __debugInfo() magic method is called when you try to dump an object using the var_dump() function. If you haven’t defined this method in your class, it’ll dump all public, private, and protected properties. So if you want to restrict the information which is displayed while dumping, you can use this method.

This method should return an array of key-value pairs which will be displayed when the var_dump() function is called on the object. As you can see, you can completely control what you want to display when the object is dumped with the var_dump() function.

### The __set_state() Method

The __set_state() method is a static method which is used in conjunction with the var_export() function. The var_export() function outputs structured information about a variable. When you use this function to export classes, you need to define the __set_state() method in your class.

As you can see, the exported string is valid PHP code, and you can use it to restore the original object.

## Conclusion

In this article, we’ve gone through all the magic methods available in PHP. For every method, I provided a short but meaningful example which should help you to understand the purpose of it. And I hope you can use this article as a quick reference or a cheatsheet in your day-to-day PHP development.

## The Best PHP Scripts on CodeCanyon

Explore thousands of the best and most useful PHP scripts ever created on CodeCanyon. With a low-cost one-time payment, you can purchase these high-quality WordPress themes and improve your website experience for you and your visitors.

Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2020.