Magic methods, which are class methods with specific names, are used to perform various specialized tasks. They are grouped into two: overloading methods and non-overloading methods. Overloading magic methods are used when your code attempts to access a method or a property which does not exist. Non-overloading methods perform other tasks.

Magic functions, which are similar to magic methods, but are just plain functions outside any class. Currently there is only one magic function in PHP.

Magic constants, which are similar to constants in notation, but act more like "dynamic" constants - their value depends on where you use them.

We'll also look at some practical examples of using some of these, and lastly we'll check out what new features PHP 5.3 is going to add.

Magic methods

For starters, let's take a look at the magic methods PHP provides. We will first go over the non-overloading methods.

__construct and __destruct

class SomeClass { public function __construct() { }

public function __destruct() { }}

The most common magic method in PHP is __construct. In fact, you might not even have thought of it as a magic method at all, as it's so common. __construct is the class constructor method, which gets called when you instantiate a new object using the new keyword, and any parameters used will get passed to __construct.

$obj = new SomeClass();

__destruct is __construct's "pair". It is a class destructor, which is rarely used in PHP, but still it is good to know about its existence. It gets called when your object falls out of scope or is garbage collected.

function someFunc() { $obj = new SomeClass(); //when the function ends, $obj falls out of scope and SomeClass __destruct is called } someFunc();

If you make the constructor private or protected, it means that the class cannot be instantiated, except inside a method of the same class. You can use this to your advantage, for example to create a singleton.

__clone

The __clone method is called when you use PHP's clone keyword, and is used to create a clone of the object. The purpose is that by implementing __clone, you can define a way to copy objects.

$obj1 = new SomeClass();$obj1->someValue = 1;

$obj2 = clone $obj1;echo $obj2->someValue;//echos 1

Important: __clone is not the same as =. If you use = to assign an object to another variable, the other variable will still refer to the same object as the first one! If you use the clone keyword, the purpose is to return a new object with similar state as the original. Consider the following:

$obj1 = new SomeClass();$obj1->someValue = 1;

$obj2 = $obj1;$obj3 = clone $obj1;

$obj1->someValue = 2;

What are the values of the someValue property in $obj2 and $obj3 now? As we have used the assign operator to create $obj2, it refers to the same object as $obj1, thus $obj2->someValue is 2. When creating $obj3, we have used the clone keyword, so the __clone method was called. As __clone creates a new instance, $obj3->someValue is still the same as it was when we cloned $obj1: 1.

If you want to disable cloning, you can make __clone private or protected.

__toString

The __toString method is called when PHP needs to convert class instances into strings, for example when echoing:

$obj = new SomeClass();echo $obj;//will output 'someclass'

This can be a useful example to help you identify objects or when creating lists. If we have a user object, we could define a __toString method which outputs the user's first and last names, and when we want to create a list of users, we could simply echo the objects themselves.

__sleep and __wakeup

These two methods are used with PHP's serializer: __sleep is called with serialize(), __wakeup is called with unserialize(). Note that you will need to return an array of the class variables you want to save from __sleep. That's why the example class returns an array with _someVar in it: Without it, the variable will not get serialized.

$obj = new SomeClass();$serialized = serialize($obj);//__sleep was called

unserialize($serialized);//__wakeup was called

You typically won't need to implement __sleep and __wakeup, as the default implementation will serialize classes correctly. However, in some special cases it can be useful. For example, if your class stores a reference to a PDO object, you will need to implement __sleep, as PDO objects cannot be serialized.

As with most other methods, you can make __sleep private or protected to stop serialization. Alternatively, you can throw an exception, which may be a better idea as you can provide a more meaningful error message.

An alternative to __sleep and __wakeup is the Serializable interface. However, as its behavior is different from these two methods, the interface is outside the scope of this article. You can find info on it in the PHP manual.

__set_state

This method is called in code created by var_export. It gets an array as its parameter, which contains a key and value for each of the class variables, and it must return an instance of the class.

$obj = new SomeClass();$obj->someVar = 'my value';var_export($obj);

This code will output something along the lines of:

SomeClass::__set_state(array('someVar'=>'my value'));

Note that var_export will also export private and protected variables of the class, so they too will be in the array.

Overloading methods

Now that we've gone through all the non-overloading methods, we can move to the overloading ones.

If you define an overloading magic method, they all have some behavior that's important to know before using them. They only apply to methods and variables that are inaccessible:

methods or variables that do not exist at all

variables which are outside the scope

Basically, this means that if you have a public member foo, overloading methods will not get called when you attempt to access it. If you attempt to access member bar, which does not exist, they will.

Also, if you declare a private/protected variable $hiddenVar, and attempt to access it outside the class' own methods, the overloading methods will get called. This applies to classes which inherit the original— any attempt to access the parent's private variables will result in overloading method calls.

__call

class SomeClass { public function __call($method, $parameters) {

}}

This magic method is called when the code attempts to call a method which does not exist. It takes two parameters: the method name that was being called, and any parameters that were passed in the call.

$obj = new SomeClass();$obj->missingMethod('Hello');//__call is called, with 'missingMethod' as the first parameter and//array('Hello') as the second parameter

This can be used to implement all kinds of useful things. For example, you can use __call to create automatic getter methods for variables in your class:

public function __call($method, $params) { if(substr($method, 0, 3) == 'get') { //Change the latter part of the method name to lowercase //so that it matches the keys in the data array return $this->_data[strtolower(substr($method, 3))]; } }}

$obj = new GetterClass();echo $obj->getBar();//output: foo

You could also use this to emulate mixins. By storing mixin classes inside a variable in the class, you could use __call to check each of them for a method that's not in the main class.

If you implement __call, any call that attempts to use a method which does not exist will go into it. You should always throw an exception in the end of __call if the method was not handled. This will help prevent bugs that can occur, if you mistype a method name and it goes into call which could silently ignore it.

If you, for some reason, want to create a method with a PHP reserved word as its name, you can fake one using __call. For example, normally you can't have a method called "function" or "class", but with __call it's possible.

__get and __set

class SomeClass { public function __get($name) { }

public function __set($name, $value) { }}

The __get and __set pair is called when attempting to read or write inaccessible variables, for example:

$obj = new SomeClass(); $obj->badVar = 'hello'; //__set is called with 'badVar' as first parameter and 'hello' as second parameter

echo $obj->otherBadVar; //__get is called with 'otherBadVar' as the first parameter

__get works similar to __call, in that you can return a value from it. If you don't return a value from __get, the value is assumed to be null.

These two can be used to, for example, creating read-only variables, or C#-style properties which run some code when being read or written.

Words of warning about magic methods

While it's possible to do fun things with magic methods, like making setting variables actually call methods, and even completely nonsensical things like making unset ($obj->foo) echo the value of $foo, keep in mind that while they are useful, it's easy to go overboard and make something that's more difficult to maintain because it's confusing.

Magic functions

This topic should probably be just called "magic function", since at the moment there is only one.

__autoload

You can define a function called __autoload to implement a default autoloader. Normally you would need to call spl_autoload_register to register any autoloader functions or methods, but not with __autoload.

function __autoload($class) { require_once $class . '.php';}

$obj = new SomeClass();//assuming SomeClass isn't defined anywhere in the code,//the __autoload function is now called with 'SomeClass' as its parameter

If you have more complex autoloading logic, you may want to just implement it as a class, similar to Zend_Loader in the Zend Framework, and then register the autoloader method manually with spl_autoload_register or a special method of the class.

Magic constants

Magic constants are special predefined constants in PHP. Unlike other predefined constants, magic constants have a different value depending on where you use them. All of them use a similar naming style: __NAME__ - that is, two underscores, name in upper case, and then two more underscores.

Here are all the magic constants:

Constant

Description

Example value

__FILE__

The currently executing file

/var/www/index.php

__LINE__

The current line

10

__FUNCTION__

The current function

myFunction

__CLASS__

The current class

MyClass

__METHOD__

The current method

MyClass::myMethod

__COMPILER_HALT_OFFSET__

The byte offset in the file, where __halt_compiler() was called

10

You can use __FUNCTION__ inside class methods. In this case, it will return just the method's name, unlike __METHOD__, which will always return the name with class name prefixed.

These constants are mainly useful for using as details in error message, and to assist in debugging. However, there are a few other uses as well.

Here's an example of throwing an exception with magic constants in the message: throw new Exception('Error in file ' . __FILE__. 'on line ' . __LINE__);

It's worth noting that exception backtraces already come with the file and line.

__FILE__ can be used to get the current script's directory:

dirname(__FILE__);

With __CLASS__, it's possible to determine a parent class' name:

class Parent { public function someMethod() { echo __CLASS__; }}

class Child extends Parent {}

$obj = new Child();$obj->someMethod();//Will output: Parent

Finally, the more special __COMPILER_HALT_OFFSET__ can only be used when a file contains a call to __halt_compiler(). This is a special PHP function, which will end the compiling of the file at the point where the function is called. Any data after this will be ignored. You could use this, for example, to store some additional data. You can then read this data by reading the current file, and looking at __COMPILER_HALT_OFFSET__. The usage of this approach is outside the scope of this article, so check the PHP manual for more details.

PHP 5.3 magic features

Lastly, let us check out what new magic methods and constants PHP 5.3 brings to the table!

New magic constants in PHP 5.3

__NAMESPACE__ contains the current namespace, which is a new feature of PHP 5.3.

New magic functions in PHP 5.3

PHP 5.3 does not add any new magic functions.

Summary

PHP contains magic methods, magic constants and a magic function, which are special features of the PHP language and can be used for various purposes. They are some of the features in PHP that makes it stand out from the crowd, as not many others provide similar capabilities, and as such, any serious PHP programmer should know them.

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.