Sunday , 17 December 2017
Home / Programming / PHP / Magic Methods – PHP OOP Tutorial
Magic Methods
Magic Methods

Magic Methods – PHP OOP Tutorial

This tutorial is a part of the PHP OOP tutorial series. If you would like to view the complete list of tutorials under this series and the order you should follow, then click here. http://jaskokoyn.com/php-oop-tutorial-series/ 

PHP provides some magic methods that run when an event occurs. You’ve already seen an example of a magic method with the __construct() method. This method runs when a user creates an instance of the Pet class.  Let’s take a look at some of the other magic methods we can use in our classes.

Using Magic Methods

In your pet.class.php file, add these methods in your Pet class. It’s quite a handful.

Now,  in our index.php file, update your code to this.

A couple of things are happening here. Let’s go over each method first.

__get()

This magic method runs when you try accessing a property that doesn’t exist in the class. It accepts 1 parameter which is the name of the property you tried accessing.

__set()

This magic method runs when you try creating or updating a property that doesn’t exist in the class. It accepts 2 parameters which is the name and the value of the property you attempted creating or updating.

__call()

This magic method runs when you try using a method that doesn’t exist. It accepts 2 parameters which is the name of the method and the parameters you tried passing in. The parameters are converted into 1 array.

__toString()

This magic method runs when you try echoing out the object itself. If you define this method, you must return a string  and not echo anything.

__clone()

This magic method runs when you try cloning an instance of the class. To clone an instance, you use the keyword clone. If you look in your index.php file, you’ll see an example of how this is done. By cloning an instance, you’re creating an object with the same properties and methods of the cloned object. They become 2 separate instances after that. This is useful when you want to save an instance at a certain state just in case.

__invoke()

This magic method runs when you try running an object as a method.

__destruct()

This magic method runs when your object is destroyed. By default, all objects are destroyed when the script is finished running. If you would like to destroy an object earlier, then you can use the unset() function. Here’s an example of how that would look like.

If you try refreshing your index.php file in your browser, then you should see a bunch of messages appear. This what you should see.

foo property does not exist.
foo property can not be set to 5 as it does not exist.
foo method does not existArray ( [0] => 5 [1] => 10 [2] => Foo )
You have created a clone of Jack
You can not run this object as a method.
The name of this object is Jack
Jack was destroyed.
Jack was destroyed.

If you look inside these methods, all we’re doing is echoing a message about what’s happening.

Conclusion

These magic methods can be used to debug your class. They can be very helpful in your future projects. You don’t have to use every magic method there is, but they can help for handling certain situations. If you would like more information about magic methods, then go here.

http://php.net/manual/en/language.oop5.magic.php

About Jasko Koyn

Check Also

Static Methods and Properties

Static Methods and Properties – PHP OOP Tutorial

Let’s learn how to create static methods and properties. Anything that’s static is basically something …

2 comments

  1. Very nice thank you!
  2. I was pulling out my god damned hair watching a Lynda OOP & PHP tutorial about __get and __set and these other magic methods. I watched it 5 times and still was going out my F***ing mind. You clarified it with one page!

    Thank You!

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link