- Joined
- 11 yrs. 6 mth. 26 days
- Messages
- 5,381
- Reaction score
- 18,380
- Age
- 45
- Wallet
- 11,590$
- [email protected]
Introduction:
To begin with, magic methods most basically said represent functions (or better know as methods) that are executing automatically under certain circumstances. The idea is that you define their usage in the code and they are also never directly called meaning that they run regardless of any restrictions. Also I'd like to mention it here that magic methods in PHP differ significantly from their usage in other object-oriented coding under other languages. There is an exact number of those magic methods and they are:
Also, note that magic methods can only be used inside classes.
__construct()
__destruct()
__call()
__callStatic()
__get()
__set()
__isset()
__unset()
__sleep()
__wakeup()
__toString()
__invoke()
__set_state()
__clone()
How it works?
However, in this part of the series of my object-oriented programming tutorials, I'll be going only through the main methods because frankly speaking (without any intended behavior) I don't think most of the people here would actually use the ones that I won't be explaining here. So let's get started.
As I always use to begin my explanations with an example let's take the following basic class and explain how __get() can be used within it. We have one property called data and is equal to the string 'Keeper'.
We create a new instance of the class and call data. Obviously, the result will be 'Keeper'. But what if we make a call to a property that simply does not exist in our class..
We don't get an error but an empty result. By using __get(), we can objectify on the non-existent property that is being called. Besides that, the __get() method does not output any notice or error if there isn't such case. It is being executed only when the certain event occurs. And here is how our class would look like:
The method is executed automatically and the $name parameter holds the name of the property that is not existent and logically, we will get output the result:
Okay. The __set() method is used when we try to append or write data to a non-existent property. So, for example, we try to add the value 'Infamous' to the data property after the instance. Then we just rewrite the previously defined value. Everything is running as expected.
But if we try to add a property that doesn't exist in our class and append a value to it, this is where the usage of __set() method comes in handy. Normally, we won't get any error if we haven't used __set() (same as with __get()). Here comes the time for me to add something that is probably the most important thing in this tutorial. When trying to write a property that doesn't exist and assign a value to it, it automatically gets included dynamically in our code in run-time. That is specific for PHP alone. As I said in the beginning, magic methods differ a lot in PHP than in some other programming languages. So up to our code:
So what we practically do store the property that is being included by a third-party and its value to an array. That very situation of the code is called registry as a whole because that array can be used to extract the data or append new one to it. In this case, this doesn't break our code or cause conflicts between properties carrying the same name and so on.
_call() itself has pretty much the same functionality and usage as __get() and __set() but with one exception/difference. It refers to methods rather than properties (as __get() and __set() did).
Upon execution of this code we get an error stating that we try to access an undefined method in an object context.
To begin with, magic methods most basically said represent functions (or better know as methods) that are executing automatically under certain circumstances. The idea is that you define their usage in the code and they are also never directly called meaning that they run regardless of any restrictions. Also I'd like to mention it here that magic methods in PHP differ significantly from their usage in other object-oriented coding under other languages. There is an exact number of those magic methods and they are:
Also, note that magic methods can only be used inside classes.
__construct()
__destruct()
__call()
__callStatic()
__get()
__set()
__isset()
__unset()
__sleep()
__wakeup()
__toString()
__invoke()
__set_state()
__clone()
How it works?
However, in this part of the series of my object-oriented programming tutorials, I'll be going only through the main methods because frankly speaking (without any intended behavior) I don't think most of the people here would actually use the ones that I won't be explaining here. So let's get started.

As I always use to begin my explanations with an example let's take the following basic class and explain how __get() can be used within it. We have one property called data and is equal to the string 'Keeper'.
We create a new instance of the class and call data. Obviously, the result will be 'Keeper'. But what if we make a call to a property that simply does not exist in our class..
We don't get an error but an empty result. By using __get(), we can objectify on the non-existent property that is being called. Besides that, the __get() method does not output any notice or error if there isn't such case. It is being executed only when the certain event occurs. And here is how our class would look like:
The method is executed automatically and the $name parameter holds the name of the property that is not existent and logically, we will get output the result:

But if we try to add a property that doesn't exist in our class and append a value to it, this is where the usage of __set() method comes in handy. Normally, we won't get any error if we haven't used __set() (same as with __get()). Here comes the time for me to add something that is probably the most important thing in this tutorial. When trying to write a property that doesn't exist and assign a value to it, it automatically gets included dynamically in our code in run-time. That is specific for PHP alone. As I said in the beginning, magic methods differ a lot in PHP than in some other programming languages. So up to our code:
So what we practically do store the property that is being included by a third-party and its value to an array. That very situation of the code is called registry as a whole because that array can be used to extract the data or append new one to it. In this case, this doesn't break our code or cause conflicts between properties carrying the same name and so on.

Upon execution of this code we get an error stating that we try to access an undefined method in an object context.