Believe it or not every class created with PHP has a number of magic methods. These are a group of class methods that PHP automatically calls during the execution of a script. Which is good, but we get to hijack them which is great.

Let’s consider this code

class MY_Account {
    private $balance;

    public function __construct( $initial_balance = 0 ) {
        $this->balance = $initial_balance;
    }
}

$account = new MY_Account( 100 );

In the above code we create a straight forward account class that keeps track of our balance. It has a method __construct that accepts and sets the initial balance for our account. The trick here and why its a magic method is that we don’t ever call __construct from the main code block.

PHP calls the __construct magic method when an instance of the class is created. We can pass arguments to it which our method uses to initialize the class properties.

I tend to use the magic methods a good deal. Especially the __construct method which allows me to initialize all of the class properties when I create the class initially.

Magic Methods

From php.net here are the definitions of the magic methods and. You can view more here.

public function __construct()

PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

This is a great method to use when initializing your class. With the ability to pass in variables to the constructor we can perform any actions that are needed.

public function __destruct()

PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.

This is our clean up method. This is an opportunity to manage the class information prior to the end of the PHP script.

public function __call( $name, $arguments )

triggered when invoking inaccessible methods in an object context.

$name – the name of the function called
$arguments – the arguments passed to the function

Keep in mind that this is only called when the caller does not have access to run this function normally. Depending on what you do in the call function you can expose private or protected methods when you shouldn’t.

public static function __callStatic( $name, $arguments )

is triggered when invoking inaccessible methods in a static context.

$name – the name of the function called
$arguments – the arguments passed to the function

This is the same as the __call method except it is run with static methods.

__get( $name )

utilized for reading data from inaccessible properties.

$name – the name of the property requested

Keep in mind that this is only called when the caller does not have access to access this property normally. Depending on what you do in the call function you can expose private or protected properties when you shouldn’t.

__set( $name, $value )

run when writing data to inaccessible properties.

$name – the name of the property that the user is try to change
$value – the value that the user is trying to give the property

__isset( $name )

triggered by calling isset() or empty() on inaccessible properties.

$name – the name of the property the user is checking

__unset( $name )

is invoked when unset() is used on inaccessible properties.

$name – the name of the property the user is try to unset

__sleep()

serialize() checks if your class has a function with the magic name __sleep(). If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn’t return anything then NULL is serialized and E_NOTICE is issued.

__wakeup()

Conversely, unserialize() checks for the presence of a function with the magic name __wakeup(). If present, this function can reconstruct any resources that the object may have.

__toString()

allows a class to decide how it will react when it is treated like a string. For example, what echo $obj; will print. This method must return a string, as otherwise a fatal E_RECOVERABLE_ERROR level error is emitted.

__invoke( [$ … ] )

called when a script tries to call an object as a function.

__set_state()

This static method is called for classes exported by var_export() since PHP 5.1.0.

__clone()

Once the cloning is complete, if a __clone() method is defined, then the newly created object’s __clone() method will be called, to allow any necessary properties that need to be changed.