A brief overview of some Magic Methods in PHP5 Object-Oriented Programing.
Welcome to this screencast about magic methods in PHP. You can find the official documentation at http://www.php.net/manual/en/language.oop5.magic.php. Please note that the naming convention of a magic method in PHP is that its name begins with a double underscore and also that a magic method should always be public.
That said I am going to create a new class, that I will call magic. I will give it some properties, respectively name, set to John, and age, set to 12.
Now I am going to create a variable called underscore that will instantiate the magic class into a new object. And I am going to create a string to test that my object has been instantiated successfully. Let's test in a browser, and it works! John is 12 years old.
Now let's see our first magic method, which is the constructor. Let's say that every time I want to call my class magic into an object, I want to choose different name and age that I will set in the instantiation. I am going to create a method called double underscore construct expecting the parameters name and age, and setting those to my object properties. And now I am going to add parenthesis after my instantiation declaration with the parameters I want, here Jack and 30. Let's test in the browser, and it works!
Now let's say I want to protect my object properties name and age, using the protected keywords. Now if I go back to my browser, it will throw an error because I am trying to access protected properties, which PHP rightfully sees as an illegal operation. So I am going to use an other magic metod, called double underscore get, that PHP will call every time an object instantiation is using a protected or a not-found property. The double underscore get magic method is expected the key argument. In my function body, I will just allow the protected property to be accessed. Now I go back to my browser and it works.
Now let's imagine that in runtime, after my object has been constructed, I want to change the name property, let's say to Peter. PHP will throw an error because I am trying to set a property that is either protected or not found - in this case, protected. To fix that, I am going to use another magic method, called double underscore get, waiting for a key and a value parameter, and in my function body, I will just allow the protected property to be set to value. Now I go back to my browser and the new name has been applied.
Now I am going to create a regular method, called walk. Upon calling, it will display a message. I call the walk function in my runtime and, back to my browser, you can see that the message is successfully displayed. Now I am going to call a method that hasn't been set, let's say the method eat. As you can see on the browser, PHP throws an error because you are calling a method that hasn't been set. So we are going to add in our class another magic method, called double underscore call. The same as double underscore get that is called everytime you are trying to access an unknown property, double underscore call is called when you are calling a method that does not exist. double underscore call takes two arguments, the name of the method and an array made of its arguments. Here, in the function body, I am just going to display a message saying that this method does not exist. Back to my browser, and the double underscore call magic method get called successfully.
Now we are going to see what happened if I am trying to echo my object. As you can see, PHP throws a fatal error because echo can only print strings and not objects. To be able to print something every time my object is called as a string, I am going to use the double underscore toString magic method. This method is expected to return a string. Let's test it in our browser: it works!
Similar to the double underscore toString magic method is the double underscore invoke magic method. This method is going to be called every time you append parenthesis to your object instantiation variable. In the function body I am just going to output the name of the object.
The last magic method I am going to show you concerns what happens when your object gets destroyed. As you probably know, when PHP has finished executing a script, it destroys all the variables that have been created during runtime, including objects. This is called garbage collection and its goal is to free memory. And when that happens, if your class has a destructor magic method, it will be called. It will also be called if you destroy the object yourself during runtime, which is what I am doing here using the unset function. Now in my function body, I am just going to echo a message. Let's test it in our browser. We can see that our destructor magic method has been called upon object unsetting.
This is all for now but you can find more PHP magic methods at http://www.php.net/manual/en/language.oop5.magic.php