Magic Methods in PHP

Magic Methods in PHPImran PollobBlockedUnblockFollowFollowingFeb 5Photo by Aziz Acharki on UnsplashThe methods which begin with two underscores (__) are called Magic Methods in PHP, and they play an important role.

PHP reserves all function names starting with __ as magical.

It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.

Lets begin .

__constructThe PHP constructor is the first method that is automatically called after the object is created.

Each class has a constructor.

If you do not explicitly declare it, then there will be a default constructor with no parameters and empty content in the class.

Constructors are usually used to perform some initialization tasks, such as setting initial values ​​for member variables when creating objects.

Parent constructors are not called implicitly if the child class defines a constructor.

In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.

If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private)__destructDestructor is the opposite of constructor.

The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.

Destructor allows you to perform some operations before destroying an object, such as closing a file, emptying a result set, and so on.

In general, the destructor is not very common in PHP.

It’s an optional part of a class, usually used to complete some cleanup tasks before the object is destroyed.

Like constructors, parent destructors will not be called implicitly by the engine.

In order to run a parent destructor, one would have to explicitly call parent::__destruct() in the destructor body.

Also like constructors, a child class may inherit the parent’s destructor if it does not implement one itself.

__callTriggered when invoking inaccessible methods in an object context, simply when method is not found in that classThis method takes two parameters.

The first parameter $name argument is the name of the method being called and the second $arguments will receive multiple arguments of the method as an array.

__callStaticTriggered when invoking inaccessible methods in a static context, simply when static method is not found in that classThis method takes two parameters.

The first parameter $name argument is the name of the method being called and the second $arguments will receive multiple arguments of the method as an array.

__getWe can use the magic method __get() to access a private property of an external object__setIt is used to set the private property of the object.

When an undefined property is assigned, the __set() method will be triggered and the passed parameters are the property name and value that are set.

__issetIt is triggered when we call isset() or empty() on inaccessible properties__unsetIt is triggered when we call unset() on inaccessible properties__sleepThe serialize() method will check if there is a magic method __sleep() in the class.

If it exists, the method will be called first and then perform the serialize operation.

The __sleep() method is often used to specify the properties that need to be serialized before saving data.

If there are some very large objects that don’t need to be saved all, then you will find this feature is very useful.

It 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 E_NOTICE is issued.

It is not possible for __sleep() to return names of private properties in parent classes__wakeupIn contrast to the __sleep() method, the __wakeup() method is often used in deserialize operations, such as re-building a database connection, or performing other initialization operations.

This will invoke destruct method internally__toStringThe __toString() method will be called when using echo method to print an object directly.

This method must return a string, otherwise it will throw a fatal error.

__invokeThis method is called when a we try to call an object as a function.

__set_stateThis 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 array(‘property’ => value, …).

__debuginfoThis 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.

__cloneAn object copy is created by using the clone keyword (which calls the object’s __clone() method if possible).

An object’s __clone() method cannot be called directly.

When an object is cloned, PHP will perform a shallow copy of all of the object’s properties.

Any properties that are references to other variables will remain references.

Once the cloning is complete, if a __clone() method is defined, then the newly created object’s __clone() method will be called, to allow any necessary properties that need to be changed.

All methods in one exampleGitHub linkMy other posts:OOP in a nutshell.. More details

Leave a Reply