Magic methods in PHP are special methods that allow objects to react to certain events. These methods are automatically invoked by PHP on certain specific events.
NOTE
We do not call these magic methods(and trigger events) directly.
These methods are invoked on special events like calling a method, converting an object to string, accessing or setting an object property, etc.
PHP has the following magic methods-
Magic Method | Magic Method Signature | Trigger | Usage |
---|---|---|---|
__construct() | public __construct(mixed …$values): void | When an object is created. | When an object is created. |
__destruct() | public __destruct(): void | When object is destroyed. | Cleanup of object properties, or triggering related actions that need to be on object deletion. |
__call() | public __call(string $name, array $arguments): mixed | When an inaccessible or undefined method is called. | Dynamic handling of method dispatching. |
__callStatic() | public static __callStatic(string $name, array $arguments): mixed | When an inaccessible or undefined static method is called. | Dynamic handling of static method dispatching. |
__get() | public __get(string $name): mixed | When we try to access an inaccessible property or some undefined property. | For accessing property dynamically. |
__set() | public __set(string $name, mixed $value): void | When we try to set/write an inaccessible or undefined property. | For setting/assigning property dynamically. |
__isset() | public __isset(string $name): bool | When isset() is used on an inaccessible or undefined property. | Check if a dynamic property is set, and/or perform some action on that trigger. |
__unset() | public __unset(string $name): void | When unset() is used on an inaccessible or undefined property. | Remove a dynamic property. |
__sleep() | public __sleep(): array | During serialization. | Prepare the object for serialization. |
__wakeup() | public __wakeup(): array | During unserialization. | Restore the object before unserialization. |
__serialize() | public __serialize(): array | Serialization hooks. | Custom serialization logic. |
__unserialize() | public __unserialize(array $data): void | Unserialization hooks. | Custom unserialization logic. |
__toString() | public __toString(): string | When an object is treated as a string. | For defining the string representation of an object. |
__invoke() | public __invoke(…$values): mixed | When an object is called like a function. | For defining the behavior of a callable object, when an object is called like a function. |
__set_state() | public __set_state(array $properties): object | When an object is re-instantiated using eval() on the output of var_export(). | |
__clone() | public __clone(): void | When an object is cloned. | Customizing object cloning behavior. |
__debuginfo() | public __debugInfo(): array | When var_dump() is called. | For customizing debug output. |
WARNING
All magic methods start with two underscores(__).
So we should not use method names that start with two underscores(__) for any other purpose.
NOTE
All magic methods must be declared as public, except __construct(), __destruct(), and __clone().
If not declared as public, then it will raise an E_WARNING.
NOTE
If types are used while declaring magic methods, then the types should match exactly with the method signature.