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.
public function __get( $name )
echo $name . ' property does not exist. <br>';
public function __set( $name, $value )
echo $name . ' property can not be set to ' . $value . ' as it does not exist. <br>';
public function __call( $name, $params )
echo $name . ' method does not exist';
print_r( $params );
public function __toString()
return 'The name of this object is ' . $this->petName . '<br>';
public function __clone()
echo 'You have created a clone of ' . $this->petName . '<br>';
public function __invoke()
echo 'You can not run this object as a method. <br>';
public function __destruct()
echo $this->petName . ' was destroyed.<br>';
Now, in our index.php file, update your code to this.
$jack = new Pet( 'Jack' );
$jack->foo = 5;
$jack->foo( 5, 10, 'Foo' );
$clone = clone $jack;
A couple of things are happening here. Let’s go over each method first.
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.
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.
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.
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.
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.
This magic method runs when you try running an object as a method.
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.
unset( $jack );
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 (  => 5  => 10  => 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.
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.