Magic Methods

Overloading
November 14, 2018
Final Keyword
November 16, 2018

The function names __construct(), __destruct(),

__call(), __callStatic(), __get(), __set(), __isset(), __unset(),__sleep(), <__wakeup(), __toString(), __invoke(),__set,_state(), __clone() and __debugInfo() are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.

__sleep() and __wakeup()

public array __sleep ( void )
void __wakeup ( void )

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 andE_NOTICE is issued.

The intended use of __sleep() is to commit pending data or perform similar cleanup tasks. Also, the function is useful if you have very large objects which do not need to be saved completely.

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.

The intended use of __wakeup() is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.

Example #1 Sleep and wakeup

<?php
class Connection
{
protected $link;
private $dsn, $username, $password;
public function __construct($dsn, $username, $password)
{
$this->dsn = $dsn;
$this->username = $username;
$this->password = $password;
$this->connect();
}private function connect()
{
$this->link = new PDO($this->dsn, $this->username, $this->password);
}public function __sleep()
{
return array(‘dsn’, ‘username’, ‘password’);
}public function __wakeup()
{
$this->connect();
}
}?>

__toString()

public string __toString ( void )

The __toString() method 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

Example #2 Simple example

}

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

$class = new TestClass(‘Hello’);
echo $class;
?>
__invoke()
mixed __invoke ([ $… ] )
The __invoke() method is called when a script tries to call an object as a function.

Note:

This feature is available since PHP 5.3.0.

Example #3 Using __invoke()


__set_state()
static object __set_state ( array $properties )
This static method is called for classes exported by var_export() since PHP 5.1.0.

The only parameter of this method is an array containing exported properties in the form array(‘property’ => value, …).

Example #4 Using __set_state() (since PHP 5.1.0)

$obj->var2 = $an_array[‘var2’];
return $obj;
}
}

$a = new A;
$a->var1 = 5;
$a->var2 = ‘foo’;

eval(‘$b = ‘ . var_export($a, true) . ‘;’); // $b = A::__set_state(array(
// ‘var1’ => 5,
// ‘var2’ => ‘foo’,
// ));
var_dump($b);

?>

The above example will output:

object(A)#2 (2) {
  ["var1"]=>
  int(5)
  ["var2"]=>
  string(3) "foo"
}

__debugInfo() ¶
array __debugInfo ( void )
This method is called by var_dump() when dumping an object to get the properties that should be shown. If the method isn’t defined on an object, then all public, protected and private properties will be shown.

This feature was added in PHP 5.6.0.

Example #5 Using __debugInfo()

}

public function __debugInfo() {
return [
‘propSquared’ => $this->prop ** 2,
];
}
}

var_dump(new C(42));
?>
The above example will output:

object(C)#1 (1) {
[“propSquared”]=>
int(1764)
}

Sondiva Technologies
Sondiva Technologies
Sondiva Technologies started in 2011 with a vision of providing Quality based Web Hosting and Web Designing Services in Ghaziabad, Delhi NCR. Backed up by full stack developers and designers , with over 500+ Web Projects. They work on opensource Platforms like Wordpress, Magneto, Prestashop, Opencart and Core PHP. Other services are : Responsive Web Designing Domain Registration Services Dedicated & SSD Hosting Virtual Private Servers Google GSuite Mail Servers E-Commerce Shopping Portal Development Web Designing & Application Courses SEO & Digital Marketing

Leave a Reply

Your email address will not be published. Required fields are marked *

Shop Now