PHP: Magic Methods

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 MethodMagic Method SignatureTriggerUsage
__construct()public __construct(mixed …$values): voidWhen an object is created.When an object is created.
__destruct()public __destruct(): voidWhen 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): mixedWhen an inaccessible or undefined method is called.Dynamic handling of method dispatching.
__callStatic()public static __callStatic(string $name, array $arguments): mixedWhen an inaccessible or undefined static method is called.Dynamic handling of static method dispatching.
__get()public __get(string $name): mixedWhen we try to access an inaccessible property or some undefined property.For accessing property dynamically.
__set()public __set(string $name, mixed $value): voidWhen we try to set/write an inaccessible or undefined property.For setting/assigning property dynamically.
__isset()public __isset(string $name): boolWhen 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): voidWhen unset() is used on an inaccessible or undefined property.Remove a dynamic property.
__sleep()public __sleep(): arrayDuring serialization.Prepare the object for serialization.
__wakeup()public __wakeup(): arrayDuring unserialization.Restore the object before unserialization.
__serialize()public __serialize(): arraySerialization hooks.Custom serialization logic.
__unserialize()public __unserialize(array $data): voidUnserialization hooks.Custom unserialization logic.
__toString()public __toString(): stringWhen an object is treated as a string.For defining the string representation of an object.
__invoke()public __invoke(…$values): mixedWhen 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): objectWhen an object is re-instantiated using eval() on the output of var_export().
__clone()public __clone(): voidWhen an object is cloned.Customizing object cloning behavior.
__debuginfo()public __debugInfo(): arrayWhen 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.

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.