What are magic methods in PHP?


  • PHP supports multiple magic methods, those methods can be identified by two underscore prefix(__). 
  • These are special functions should be defined by the user but no need to call them explicitly. It will be called on an appropriate event occurrence. For example, class __construct() will be called while instantiating the class.
  • PHP magic methods must be defined inside the class.
Note: Declaring the constructor method private prevents external code from directly creating an object. This is handy for creating singleton classes that restrict the number of objects that can exist.

PHP Magic Methods and Purposes

Below are the magic methods invoked on creating Class Instance -

__construct() - The __construct() method is most commonly used magic method. Here you can do initialization you need when an object is created. You can define any number of arguments that will be passed when creating objects.

__destruct() - The __destruct() method is called when the object is destroyed. It accepts no arguments and is commonly used to perform any cleanup operations such as closing a database connection.

__get()/__set() - The getters and setters magic methods are used for getting and putting values for class properties created dynamically by PHP property overloading.

__isset() - These methods are invoked automatically while checking whether a required overloaded property is set or not, by using the PHP isset() function.

__unset() - PHP unset() function on such dynamically created properties, this magic method will automatically be invoked.

__call()/__callStatic() - These two magic methods are dedicated to accessing dynamically created but invisible methods on PHP method overloading. These differ, where __call() will invoke normal PHP overloaded methods, and __callStatic() will invoke static methods

Magic Methods with PHP Object Serialization

PHP function serialize()/unserialize() used in PHP Object Serialization.

sleep() - This will be invoked on calling PHP serialize() function. It will return the object’s property array on cleaning PHP class objects before serialization.

wakeup() – It will do the reverse work to restore the object's properties and resources on invoking unserialize().

Other Magic Methods in PHP

__toString() - This method is expected to return a string value while using class instances with PHP printing statements.

__invoke() - This method defined in a class will be called when we make a function call with the name of that class instance.

__set_static() - This is a static method invoked while exporting objects property array and expects such an array variable as its argument.

__clone() - This function is used while PHP Object Cloning.

Note: PHP's version of "overloading" is not quite the same as most other languages, though the same results can be reached.

Comments

Popular posts from this blog

Basic Terminology

What is normalization in DBMS?

Object oriented programming concepts in PHP