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

IDBI JAM, AAO Recruitment 2024 Registration begins for JAM/AAO posts, Apply for 600 posts here

Read More

IDBI Bank Recruitment 2024: आईडीबीआई में जेएएम और एएओ के लिए निकली भर्ती, जानें कौन कर सकता है आवेदन

Read More

CAT 2024 Tomorrow: Exam day guidelines, timings, do's and don'ts; Check the list of prohibited items here

Read More

CBSE Single Girl Child Scholarship 2024 Registration window open now, Check the eligibility criteria and more

Read More

UP Police Constable Result 2024: Candidates demand raw scores, question transparency; Check the latest update

Read More

UP Police Result: यूपी पुलिस भर्ती के अभ्यर्थी कर रहे अंक जारी करने की मांग, बोर्ड ने दी प्रतिक्रिया

Read More

RRB ALP Admit Card: 25 नवंबर की एएलपी भर्ती परीक्षा के लिए जारी हुआ प्रवेश पत्र, जानें डाउनलोड करने का तरीका

Read More

CHSE Odisha Class 12 date sheet 2025 out now; Check the exam schedule here

Read More

CBSE Date Sheet 2025: सीबीएसई बोर्ड कक्षा 10वीं 12वीं की डेटशीट हुई जारी, यहां देखें पूरा शेड्यूल

Read More