As you can see, the structure of the above "User" class is pretty easy to follow. It's only composed of a few setters and getters, used for setting and retrieving the values assigned to its three declared properties, that is $firstname, $lastname and $email respectively.
So far, nothing strange is happening here, right? What follows is a simple demonstration of how to use this class:
“User” example 2
In the previous example, I created a basic "User" class, whose API allowed us to easily assign and retrieve the values of its declared properties. However, as I expressed earlier, it's possible to shorten its definition and make it more "dynamic" simply by concretely implementing the "__set()" and "__get()" methods.
To demonstrate this concept a bit more, I'm going to redefine this sample class to allow us not only to create new class properties at run time, but retrieve their respective values with extreme ease.
Now that you know what I'm going to do in the next few lines, please examine the modified version of the "User" class. It now looks like this:
In the first case, the "__set()" function will create an undeclared property and assign a value to it, while in the last case, its counterpart "__get()" will retrieve this value if the property has been previously set.
The implementation of these two methods permits us to overload properties in a very simple way. It also makes the class's source code much shorter and more compact. The down side to this is that this process has some disadvantages that must be taken into account. First, and most importantly, the lack of an explicit declaration for each property of the class makes the code harder to read and follow. Second, it's practically impossible for an IDE like Eclipse PDT to keep track of those properties for either commenting or documenting them.
__call()
If a class implements __call(), then if an object of that class is called with a method that doesn't exist __call() is used instead as a substitute. For example:
Now let’s add a method called test() and run it again. Notice that this time test() was called instead of __call():
__sleep()
The __sleep() magic method is called when the object of a class is about to be serialized. Serialization is the process of converting an object into a linear sequence of bytes for storage or transmission to another location over a network. This magic method __sleep() does not accept any parameter and returns an array. The array should contain a list of class members that should be serialized. This means that if you don’t wish to serialize a particular class member, you should not include it in the array. Look at the example below:
In the above example, you can see that the serialized string data only contains the fname of the Employee Object. This is because the __sleep() magic method returned an array containing only the ‘fname’ data member.
__wakeup()
The __wakeup() magic method is the opposite of the __sleep() method and does not accept any parameter nor returns anything. It is responsible for setup operations after an object has been unserialized. Unserialized is the opposite of serialize which means to convert an array into a normal string that you can save in a file, pass in a URL, etc. To unserialize means to take a serialized string and convert it back to an array. The PHP functions Serialize() and Unserialize() are used to perform these operations. Look at the example below:
In the above example, you can see that after the $e object has been serialized, that is converted to a common string, and the output stored in $data variable, we use the $data variable and pass it to the unserialize(). Before the object is unserialized and object created, the __wakeup() method is called.
__clone()
In PHP, objects are always assigned and passed around by reference. Let me explain with a simple example:
Notice that the value of the $first->name property changed from “Number 1” to “Number 2” after I made a copy of the $first object called $second and set the value of $second->name=”Number 2.” This is because both the $first and $second objects actually refer to the same object, the $second object was assigned by reference.
The __clone() magic method can be used to make a copy of an object that doesn’t refer to the object being copied but instead creates a new and completely distinct object. Let me demonstrate how __clone() works by using it in the previous example instead of setting $second = $first.
This time the value of the $first->name did not change after we set a value for $second->name. That is because clone allowed us to make a copy of first that is a totally different object. Hence, now we have two objects not just two references to the same object like we had before.
Конец ознакомительного фрагмента.
Текст предоставлен ООО «ЛитРес».
Прочитайте эту книгу целиком, купив полную легальную версию на ЛитРес.
Безопасно оплатить книгу можно банковской картой Visa, MasterCard, Maestro, со счета мобильного телефона, с платежного терминала, в салоне МТС или Связной, через PayPal, WebMoney, Яндекс.Деньги, QIWI Кошелек, бонусными картами или другим удобным Вам способом.