Different PHP Magic methods

Safalta Expert Published by: Aryan Rana Updated Thu, 08 Dec 2022 10:33 PM IST

Highlights

PHP allows for the definition of special functions that can be automatically called without the need for a function call to run the code inside them. This function is offered by a unique technique known as magic methods.

PHP allows for the definition of custom functions that can be automatically invoked without the need for a function call to run the code inside them. This function is offered through a unique technique called magic ways.

Source: Safalta.com




What do PHP's magic methods do?

In PHP, methods that start with two underscores (__) are referred to as magic methods. The names of these methods are restricted to a set of reserved keywords that are supported by PHP. Therefore, it is not advisable to define any functions with the names of PHP magic methods.

Free Demo Classes

Register here for Free Demo Classes



These functions should typically be defined by the user, therefore calling them explicitly is not necessary.


List of PHP magic methods
  • __construct()
  • __destruct()
  • __call($fun, $arg)
  • __callStatic($fun, $arg)
  • __get($property)
  • __set($property, $value)
  • __isset($content)
  • __unset($content)
  • __sleep()
  • __wakeup()
  • __toString()
  • __invoke()
  • __set_state($array)
  • __clone()
  • __debugInfo()

An object's creation triggers the automatic call to the method construct(). The arguments that will be supplied when creating objects can be defined here in any number.

When there are no more references to an object, the destructor, which is a typical component of object-oriented languages, is called.

When an undefined or inaccessible method is called, the __call($fun, $arg) method is invoked. This indicates that it is called whenever an object's undefined or unavailable method is called.

When an under or unavailable method is called statically, the method __callStatic($fun, $arg) is invoked.

When using PHP property overloading, the __get($property) method is used to retrieve values for dynamically constructed class properties.

This method, __set($property, $value), is used to set values for class properties that were dynamically created using PHP property overloading.

When calling isset() or empty() for an undefined or inaccessible member, isset($content) will be called. Using the PHP isset() function, it is automatically called when determining whether a required overloaded property is set or not.

When invoking reset() for an undefined or inaccessible member, __unset($content) is invoked.

When running serialise, the __sleep() method is the first one to be invoked (). When PHP class objects are cleaned before serialisation, the object's property array is returned.

When deserialization() is running, the method __wakeup() is invoked. When deserialization is invoked, work would need to be done backwards to restore objects' properties and resources ().

The class-defined method __invoke() is used when attempting to call an object in a manner similar to calling a function.

When deserialization() is running, the method __wakeup() is invoked. When deserialization is invoked, work would need to be done backwards to restore objects' properties and resources ().

The class-defined method __invoke() is used when attempting to call an object in a manner similar to calling a function.

When an object is being dumped, the method var dump() calls __debugInfo() to retrieve the attributes that should be displayed. All of an object's public, protected, and private attributes will be displayed if the method is not specified on it.

When deserialization() is running, the method __wakeup() is invoked. When deserialization is invoked, work would need to be done backwards to restore objects' properties and resources ().

 

Related Article

5 of the most valued marketing skills

Read More

How to Increase Leads by Marketing Automation, know here

Read More

The Ultimate Guide: Everything You Need to Know for Writing Amazing

Read More

Online Marketing : The Who, What, Why and How of Digital Marketing

Read More

The Ultimate Guide to Blogger Outreach: Everything You Need to Know

Read More

How to perfect your Marketing Resume

Read More

Top 10 career opportunities after 12th

Read More

Role of communication skills & personality development in life

Read More

Best use of Information Technology for every job 

Read More