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

Nepali Student Suicide Row: Students fear returning to KIIT campus; read details here

Read More

NEET MDS 2025 Registration begins at natboard.edu.in; Apply till March 10, Check the eligibility and steps to apply here

Read More

NEET MDS 2025: नीट एमडीएस के लिए आवेदन शुरू, 10 मार्च से पहले कर लें पंजीकरण; 19 अप्रैल को होगी परीक्षा

Read More

UPSC CSE 2025: यूपीएससी सिविल सेवा परीक्षा के लिए आवेदन करने की अंतिम तिथि बढ़ी, इस तारीख तक भर सकेंगे फॉर्म

Read More

UPSC further extends last date to apply for civil services prelims exam till Feb 21; read details here

Read More

Jhakhand: CM launches six portals to modernise state's education system

Read More

PPC 2025: आठवें और अंतिम एपिसोड में शामिल रहें यूपीएससी, सीबीएससी के टॉपर्स, रिवीजन के लिए साझा किए टिप्स

Read More

RRB Ministerial, Isolated Recruitment Application Deadline extended; Apply till 21 February now, Read here

Read More

RRB JE CBT 2 Exam Date: आरआरबी जेई सीबीटी-2 की संभावित परीक्षा तिथियां घोषित, 18799 पदों पर होगी भर्ती

Read More