PHP.nl

Magic Methods

Magic Methods

Magic methods are special methods which override PHP's default's action when certain actions are performed on an object.

Let op: > All methods names starting with are reserved by PHP. Therefore, it is not recommended to use such method names unless overriding PHP's behavior. __

The following method names are considered magical:

, , , , , , , , , , , , , , , , and . __construct()__destruct()__call()__callStatic()__get()__set()__isset()__unset()__serialize()__unserialize()__sleep()__wakeup()__toString()__invoke()__set_state()__clone()__debugInfo()

Waarschuwing: > All magic methods, with the exception of , , and , be declared as , otherwise an is emitted. Prior to PHP 8.0.0, no diagnostic was emitted for the magic methods , , , , and . __construct()__destruct()__clone()mustpublic``E_WARNING__sleep()__wakeup()__serialize()__unserialize()__set_state()

Waarschuwing: > If type declarations are used in the definition of a magic method, they must be identical to the signature described in this document. Otherwise, a fatal error is emitted. Prior to PHP 8.0.0, no diagnostic was emitted. However, and must not declare a return type; otherwise a fatal error is emitted. __construct()__destruct()

__serialize() and

__unserialize()
array **__serialize**
void **__unserialize** array $data
 checks if the class has a function with
the magic name . If so, that function is
executed prior to any serialization. It must construct and return an associative array of key/value pairs
that represent the serialized form of the object. If no array is returned a 
will be thrown.

serialize__serialize()TypeError

Opmerking: > If both and are defined in the same object, only will be called. will be ignored. If the object implements the interface, the interface's method will be ignored and used instead. __serialize()__sleep()__serialize()__sleep()Serializableserialize()__serialize()

The intended use of  is to define a serialization-friendly
arbitrary representation of the object. Elements of the array may correspond to properties of the object but
that is not required.

__serialize()

Conversely,  checks for the
presence of a function with the magic name
. If present, this function will be passed the
restored array that was returned from .  It may
then restore the properties of the object from that array as appropriate.

unserialize__unserialize()__serialize()

Opmerking: > If both and are defined in the same object, only will be called. will be ignored. __unserialize()__wakeup()__unserialize()__wakeup()

Opmerking: > This feature is available as of PHP 7.4.0.

Voorbeeld: Serialize and unserialize

<?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 __serialize(): array
    {
        return [
          'dsn' => $this->dsn,
          'user' => $this->username,
          'pass' => $this->password,
        ];
    }

    public function __unserialize(array $data): void
    {
        $this->dsn = $data['dsn'];
        $this->username = $data['user'];
        $this->password = $data['pass'];

        $this->connect();
    }
}?>

__sleep() and

__wakeup()

Waarschuwing: > This serialization mechanism is soft-deprecated as of PHP 8.5.0. It is maintained for backward compatibility. However, new and existing code should be migrated to use the and

magic methods instead.

__serialize()__unserialize()

array **__sleep**
void **__wakeup**
checks if the class has a function with

the magic name . 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 is issued. serialize__sleep()E_NOTICE

Opmerking: > It is not possible for to return names of private properties in parent classes. Doing this will result in an level error. Use instead. __sleep()E_NOTICE__serialize()

Opmerking: > As of PHP 8.0.0, returning a value which is not an array from generates a warning. Previously, it generated a notice. __sleep()

The intended use of is to commit pending data or perform similar cleanup tasks. Also, the function is useful if a very large object doesn't need to be saved completely. __sleep()

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

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

Voorbeeld: 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()

string **__toString**
The  method allows a class to decide
how it will react when it is treated like a string. For example,
what  will print.

__toString()echo $obj;

Waarschuwing: > As of PHP 8.0.0, the return value follows standard PHP type semantics, meaning it will be coerced into a if possible and if

 is disabled.
`string`strict typing


 A  object will
  be accepted by a  type declaration if
 
 is enabled. If such behaviour is wanted the type declaration must accept
  and  via a union type.
`Stringable`*not*`string`strict typing`Stringable``string`


 As of PHP 8.0.0, any class that contains a 
 method will also implicitly implement the  interface, and will
 thus pass type checks for that interface.  Explicitly implementing the interface anyway is
 recommended.
__toString()`Stringable`


 In PHP 7.4, the returned value  be a
 , otherwise an  is thrown.
*must*`string``Error`


 Prior to PHP 7.4.0, the returned value  be a
 , otherwise a fatal 
 is emitted.
*must*`string``E_RECOVERABLE_ERROR`

Waarschuwing: > It was not possible to throw an exception from within a

 method prior to PHP 7.4.0. Doing so will result in a fatal error.
__toString()

Voorbeeld: Simple example

<?php
// Declare a simple class
class TestClass
{
    public $foo;

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

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

$class = new TestClass('Hello');
echo $class;
?>
Hello

__invoke()

mixed **__invoke**  $values
The  method is called when a script tries to
call an object as a function.

__invoke()

**Voorbeeld: Using **

<?php
class CallableClass
{
    public function __invoke($x)
    {
        var_dump($x);
    }
}
$obj = new CallableClass;
$obj(5);
var_dump(is_callable($obj));
?>
int(5)
bool(true)

**Voorbeeld: Using **

<?php
class Sort
{
    private $key;

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

    public function __invoke(array $a, array $b): int
    {
        return $a[$this->key] <=> $b[$this->key];
    }
}

$customers = [
    ['id' => 1, 'first_name' => 'John', 'last_name' => 'Do'],
    ['id' => 3, 'first_name' => 'Alice', 'last_name' => 'Gustav'],
    ['id' => 2, 'first_name' => 'Bob', 'last_name' => 'Filipe']
];

// sort customers by first name
usort($customers, new Sort('first_name'));
print_r($customers);

// sort customers by last name
usort($customers, new Sort('last_name'));
print_r($customers);
?>
Array
(
    [0] => Array
        (
            [id] => 3
            [first_name] => Alice
            [last_name] => Gustav
        )

    [1] => Array
        (
            [id] => 2
            [first_name] => Bob
            [last_name] => Filipe
        )

    [2] => Array
        (
            [id] => 1
            [first_name] => John
            [last_name] => Do
        )

)
Array
(
    [0] => Array
        (
            [id] => 1
            [first_name] => John
            [last_name] => Do
        )

    [1] => Array
        (
            [id] => 2
            [first_name] => Bob
            [last_name] => Filipe
        )

    [2] => Array
        (
            [id] => 3
            [first_name] => Alice
            [last_name] => Gustav
        )

)

__set_state()

object **__set_state** array $properties
This  method is called
for classes exported by .

staticvar_export

The only parameter of this method is an array containing exported
properties in the form .

['property' =&gt; value, ...]

**Voorbeeld: Using **

<?php

class A
{
    public $var1;
    public $var2;

    public static function __set_state($an_array)
    {
        $obj = new A;
        $obj->var1 = $an_array['var1'];
        $obj->var2 = $an_array['var2'];
        return $obj;
    }
}

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

$b = var_export($a, true);
var_dump($b);
eval('$c = ' . $b . ';');
var_dump($c);
?>
string(60) "A::__set_state(array(
   'var1' => 5,
   'var2' => 'foo',
))"
object(A)#2 (2) {
  ["var1"]=>
  int(5)
  ["var2"]=>
  string(3) "foo"
}

Opmerking: > When exporting an object, does not check whether is implemented by the object's class, so re-importing objects will result in an exception, if __set_state() is not implemented. Particularly, this affects some internal classes. var_export__set_state()Error

It is the responsibility of the programmer to verify that only objects will be re-imported, whose class implements __set_state().

__debugInfo()

array **__debugInfo**
This method is called by  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.

var_dump

**Voorbeeld: Using **

<?php
class C {
    private $prop;

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

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

var_dump(new C(42));
?>
object(C)#1 (1) {
  ["propSquared"]=>
  int(1764)
}