PHP OOP With Magic Methods

In this tutorial we will learn about exclusive topic in php oop which is Magic Methods. Any class that you create in php has some built in methods. Those methods are special methods that allow you to react to certain events during class life cycle.

Magic methods begin with __ and any one of us already came across a number of these methods in classes specially the two important __construct() and __destruct() so let’s discover these.

 

__construct() and __destruct()

The most common of these is the __construct() and __destruct(). __construct()  is called automatically when you create a new instance of the class but __destruct() is called when the object execution life cycle is terminated or destroyed.

 

__construct can be used to initialize variables and in the context of dependency injection but __destruct() can be used to do things like garbage memory resources or unsetting values that already created by the constructor. One real world examples of these is to open and close a database connection.

 

Let’s see it in action:

In the example above note that when we create a new instance of class Post it called the two magic methods __construct() and __destruct().

 

__get() and __set()

Usually in php there are certain circumstances when you need to access a property directly by its name but because the property you want to access declared as protected or private it will cause error so to access it you have to implement  __get() and __set().

The __get() method will be called  automatically for properties that are not public properties. The __get() method accepts the name of the property you were looking for as an argument. In the code above, first I check to see if the property exists on the current object. If it does, I can return it dynamically from the object.

 

If you try to set a property that is not accessible, the __set() magic method will be called. This method takes the property you were attempting to access and the value you were trying to set as two arguments.

In these two examples I’ve shown how you can get or set properties on an object that are not set to public. Always try to use these methods in your own projects instead of creating custom getters or setters.

__isset() and __unset()

You already encountered the isset() function in a lot of scripts when working with arrays to check if specific key is exist. You can also use this function on objects to see if a publicly accessible property has been set.

If you attempt to use this function to check the presence of a property that isn’t publicly accessible, you can use the __isset() magic method to respond:

From the above code true will be returned because when you call isset() on the title property it will call __isset() behind the scenes and return true.

 

Similar to the isset() function, the unset() function is commonly used when working with arrays. Again, as with the isset() function, you can also run it on an object to unset public properties. If the property you are trying to unset is not public, the __unset() method will will be triggered and allow you to implement it:

 

__toString()

The __toString() allows you to return a string representation of an object. If you work with one of the most common frameworks like Laravel or Yii you will encounter it in the Model classes.

This means whenever you try to cast the object as a string, such as trying to use echo, the object will be returned however you define it in the __toString() method.

 

 

__sleep() and __wakeup()

The serialize() function is a pretty common way to store a representation of an object. For example, if you wanted to store an object in the database, first you would serialize it, store it, and then when you wanted it again you would unserialize it.

The __sleep() method allows you to define which properties of the object should be serialized as you probably don’t want to serialize any kind of external object that might not be relevant when you unserialize the object.

When it comes to unserializing the object, or recovering it we use __wakeup() method.

__call()

The __call() method will be called when you try to access a method that is not publicly accessible on the object. For example you might have an array of data on the object that you want to mutate before returning:

In the above code method getTitle() and getDescription() is defined private however because we implement __call() it will be called.

 

__clone()

When you make a copy of an object in PHP, it is still linked to the original object. This means if you make a change to the original object, your copied object will also be changed:

This is because when you copy an object in PHP, it is “passed as a reference”, meaning, it maintains a link to the original object However if you to create to distinct objects you have to implement __clone():

 

__invoke()

The __invoke() magic method allows you to use an object as if it were a function:

 

conclusion

In this tutorial you learned about php magic methods and how to use them. Many frameworks make use of magic methods as it provides many benefits and away of clean code and maintainability.

Share this: