Magic Methods

These are builtin methods provided by PHP that allow additional functionality in OOP.

 

  • start with two underscores e.g. __construct()
  • automatically called under specific conditions

 

So far the above examples have used the following:

  • __construct()
  • __destruct()
  • __clone()
  • __sleep()
  • __wakeup()

 

The following examples now explore the remaining major magic methods:

  • __autload()
  • __get()
  • __set()
  • __call()
  • __tostring()

 

__autoload()

Sometimes there may be a large number of include files required for multiple classes, which could become unmanageable as the number of separate class include files grows, and you might not want to include every file 'just in case'.

 

PHP has a facility to automatically load files by using the __autoload() function, that attempts to load a (perhaps forgotten include) file based on the name of the object trying to be created.

 

A typical __autoload() function syntax:

 

function __autoload($class_name) {

include 'class.' . $class_name . 'inc';

}

 

If a new object were trying to be created, say using $fido = new Dog; and that class had not been defined/included, then the __autoload function would attempt to load class.Dog.inc

 

*note: the autoloading filename is case sensitive and may require the strtolower() function, if your file names are in lower case.

 

Since the autoloading filename is case sensitive, this example uses the previous person.inc and therefore requires the strtolower() function to convert the Person class name to lower case for the person.inc filename:

Save & refresh browser:

Attempting to load person.inc
Object instantiated at 1372952352
Hi, my name is Jim and I was created at 1372952352.
 

Person::__set_state(array(
'name' => 'Jim',
'created' => 1372952352,
))

 


__get()

Used within a class when an attempt is made to access a property that isn't accessible e.g. if it doesn't have public visibility or doesn't exist.

Save & refresh browser:

Access to private property using the __get() magic method called on name: James Tiberius Kirk
__get() magic method called on age:
 

Person::__set_state(array(
'name' => 'James Tiberius Kirk',
))

 


__set()

Used within a class when a call is made to set a property that isn't accessible e.g. if it doesn't have public visibility or doesn't exist.

Save & refresh browser:

__get() magic method called on name: James Tiberius Kirk
Access to private property __set() magic method called!
__get() magic method called on data: Mr Spock
 

Person::__set_state(array(
'name' => 'James Tiberius Kirk',
))

 


__call()

Used within a class when a call is made to a method that isn't accessible e.g. if it doesn't have public visibility or doesn't exist.

 

This example has commented out the fly() method, on lines 5-8, to invoke the __call() magic method:

Save & refresh browser:

Sorry, Peter Parker, Call to fly() with args 'Only, Superman, can, fly!' failed!

 


__tostring()

Defined within a class to decide what an object should do when treated like a string (e.g. echo $myObject;) and must return a string:

Save & refresh browser:

Hi Peter Parker, here's the string for the new Person object!

Leave a Reply