|
Magic MethodsMagic methods are special methods which override PHP's default's action when certain actions are performed on an object. Caution
All methods names starting with The following method names are considered magical: __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __serialize(), __unserialize(), __toString(), __invoke(), __set_state(), __clone(), and __debugInfo(). Warning
All magic methods, with the exception of
__construct(),
__destruct(), and
__clone(),
must be declared as Warning
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, __construct() and __destruct() must not declare a return type; otherwise a fatal error is emitted. __sleep() and __wakeup()
public array __sleep()
public void __wakeup()
serialize checks if the 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
The intended use of __sleep() 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. 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
__serialize() and __unserialize()
public array __serialize()
public void __unserialize(array
$data )serialize checks if the class has a function with the magic name __serialize(). 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 TypeError will be thrown.
The intended use of __serialize() 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. Conversely, unserialize checks for the presence of a function with the magic name __unserialize(). If present, this function will be passed the restored array that was returned from __serialize(). It may then restore the properties of the object from that array as appropriate.
Example #2 Serialize and unserialize
__toString()
public string __toString()
The __toString() method allows a class to decide
how it will react when it is treated like a string. For example,
what Warning
As of PHP 8.0.0, the return value follows standard PHP type semantics, meaning it will be coerced into a string if possible and if strict typing is disabled. A Stringable object will not be accepted by a string type declaration if strict typing is enabled. If such behaviour is wanted the type declaration must accept Stringable and string via a union type. As of PHP 8.0.0, any class that contains a __toString() method will also implicitly implement the Stringable interface, and will thus pass type checks for that interface. Explicitly implementing the interface anyway is recommended. In PHP 7.4, the returned value must be a string, otherwise an Error is thrown.
Prior to PHP 7.4.0, the returned value must be a
string, otherwise a fatal Warning
It was not possible to throw an exception from within a __toString() method prior to PHP 7.4.0. Doing so will result in a fatal error. Example #3 Simple example
The above example will output: Hello __invoke()
mixed __invoke(
...$values )The __invoke() method is called when a script tries to call an object as a function. Example #4 Using __invoke()
The above example will output: int(5) bool(true) Example #5 Using __invoke()
The above example will output: 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()
static object __set_state(array
$properties )This static method is called for classes exported by var_export.
The only parameter of this method is an array containing exported
properties in the form Example #6 Using __set_state()
The above example will output: string(60) "A::__set_state(array( 'var1' => 5, 'var2' => 'foo', ))" object(A)#2 (2) { ["var1"]=> int(5) ["var2"]=> string(3) "foo" }
__debugInfo()
array __debugInfo()
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. Example #7 Using __debugInfo()
The above example will output: object(C)#1 (1) { ["propSquared"]=> int(1764) } |