- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Magic Methods
Magic methods in PHP are special methods that are aimed to perform certain tasks. These methods are named with double underscore (__) as prefix. All these function names are reserved and can't be used for any purpose other than associated magical functionality. Magical method in a class must be declared public. These methods act as interceptors that are automatically called when certain conditions are met.
Following magical methods are currently available in PHP
public __sleep ( void ) : array
serialize() method in class checks if it has a function name __sleep(). If so, that function is executed prior to any serialization. It is supposed to return an array with the names of all variables of that object that should be serialized.
public __wakeup ( void ) : void
unserialize() method checks there exists a function with the magic name __wakeup(). If present, this function can reconstruct any resources that the object may have.
public __serialize ( void ) : array
serialize() method also checks if the class has __serialize() method. If so, it 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.
public __unserialize ( array $data ) : void
unserialize() also checks for if __unserialize() is present, and it 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
public __toString ( void ) : string
The __toString() method describes string representation of object. For example, what echo $obj; will print. This method must return a string
__invoke ([ $... ] ) : mixed
This method is called when a script tries to call an object as a function.
static __set_state ( array $properties ) : object
This static method is called for classes exported by var_export(). It receices one parameter which is an array containing exported properties in the form array('property' => value, ...).
__debugInfo ( void ) : array
This method is automatically called when var_dump() is executed for dumping an object to get the properties that should be shown. If it isn't defined,all public, protected and private properties will be shown.
public __set ( string $name , mixed $value ) : void
__set() is run when writing data to inaccessible (protected or private) or non-existing properties.
public __get ( string $name ) : mixed
__get() is utilized for reading data from inaccessible (protected or private) or non-existing properties.
public __isset ( string $name ) : bool
__isset() is triggered by calling isset() or empty() on inaccessible (protected or private) or non-existing properties.
public __unset ( string $name ) : void
__unset() is invoked when unset() is used on inaccessible (protected or private) or non-existing properties.
- Related Articles
- Dunder or magic methods in python
- Where are operators mapped to magic methods in Python?
- PHP Static Properties and Methods
- Magic Square
- How do I chain methods in PHP?
- Passing static methods as arguments in PHP
- What are getters and setters methods in PHP?
- Explain the term Magic Acid?
- Calvin Klein The Magic Creator
- PHP Call methods of objects in array using array_map?
- Magic Squares In Grid in C++
- Christian Louboutin: The Magic Shoe Man
- Learn How to Perform Simple Magic Tricks
- What are the methods for sanitizing user inputs with PHP?\n
- Why is Bodh Gaya known for black magic?