A Quick view on PHP magic methods

Nov 26, 2013, by admin

Let’s have a quick view on magic methods, as sometimes it looks like it will hard to use these function but reality is these are very simple.
First of all let me tell what magic method is and why these are called magical? Are these methods really showed some type of magic as the name imply.

PHP reserves all function names starting with two underscore prefix (__) as magical.

Magic methods provide hooks into special PHP behavior.

PHP does not provide the definitions of the magic functions and the programmers have to write/code that what these functions will do. Magic functions will never directly be called by the programmer but PHP will call the function ‘behind the scenes’. That’s why they are called ‘magic’ functions because these can not be directly called and they allow the programmer to do some powerful things by the coding. I think it’s enough; you will be clearer by examples.

__autoload()
This is not a magic method exactly but it is very useful. The __autoload() function is automatically called when a class is instantiated and file will get include in the code. This is useful since you don’t always want to load every class again and again and add just in case you need it to add.

__construct()
This magic methods is called when user create object (instances of your class) of the class. Usually this is used for creating constructor in php5.

__destruct()
As the name implies, the __destruct() method is called when the object is destroyed by PHP’s garbage collector. It accepts no arguments, and it is usually used to perform memory clean-up operations such as closing a database connection or closing any file.
<?php

class House{

public function __construct() {
$this->created = time();
$this->logfile = fopen(‘/tmp/log.txt’, ‘w’);
}

public function __destruct() {
fclose($this->logfile);
}
}
$home = new House;
echo $home->created;
?>

__get
This method is called when your object try attempt to read property or variable of the class which is unavailable or inaccessible.

__set
This method called when object of your class attempts to set value of the property which is inaccessible or unavailable in your class.

__call
This magic method trigger when you are attempting to call method or function of the class which is either inaccessible or unavailable.

__callstatic
This is same as __call executes when inaccessible or unavailable method in static context.
<?php

class House
{
function __get($name)
{
echo “__get executed as $name is unavailable”;
}
function __set($name , $value)
{
echo “__set executed as $name not exists or inaccessible”;
}
function __call($name , $parameter)
{
$a = print_r($parameter , true); //taking recursive array in string
echo “__call executed with name $name , parameter $a”;

}
static function __callStatic($name , $parameter)
{
$a = print_r($parameter , true); //taking recursive array in string
echo “__callStatic executed with name $name , parameter $a”;

}
}
$a = new House();
$a->abc = 3;//__set executed
$app =  $a->myvar;//__get triggerd
$a->getMyinfo(‘samuel’ , ‘Bugtreat’, ‘blog’);//__call willl executed
House::xyz(‘1’ , ‘sca’ , ‘help’);//__callstatic will executed

?>

__isset
This magic methods called when isset() function is applied on a property of the class which is inaccessible or unavailable.

__unset
This is just opposite of isset method as it called when unset() function called on inaccessible or unavailable property of the class.
<?php

class House
{
function __isset($name)
{
echo “__isset is called for $name is unavailable”;
}
function __unset($name)
{
echo “__unset is called for $name”;
}
}
$a = new House();
isset($a->myvar);
unset($a->yourvar);

?>

__sleep
This method will trigger when you are going to serialize your class’s object.

__wakeup
This will execute when you are un-serializing any class object.
<?php

class House {
public $name;
public $width;
public $data = array(); // stores misc. data in an array
public $connection;     // holds some connection resource

public function  __sleep() {
// list the properties to save
return array(‘name’, ’56’, ‘data’);
}

public function  __wakeup() {
// reconnect to the house
$this->connect();
}
}
?>

__toString
This executes when you are using echo on your object.

__invoke
This will trigger when you are using object of your class as function.
<?php

class House
{
public $myvar;

public function __construct($myvar)
{
$this->myvar = $myvar;
}

public function __toString()
{
return $this->myvar;
}

public function __invoke($x)
{
var_dump($x);
}
}

$object = new House(‘Hello’);
echo $object; // __toString will called
$object(8); // __invoke called
?>

Hope the above one helps you a lot.