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.

Free Demo Classes

Register here for Free Demo Classes

Please fill the name
Please enter only 10 digit mobile number
Please select course
Please fill the email
Something went wrong!
Download App & Start Learning
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.


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.

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.

Source: Safalta.com

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 ().

 

Free Demo Classes

Register here for Free Demo Classes

Trending Courses

Professional Certification Programme in Digital Marketing (Batch-5)
Professional Certification Programme in Digital Marketing (Batch-5)

Now at just ₹ 49999 ₹ 9999950% off

Master Certification Digital Marketing Program Batch-10
Master Certification Digital Marketing Program Batch-10

Now at just ₹ 64999 ₹ 12500048% off

Advanced Certification in Digital Marketing Online Programme (Batch-22)
Advanced Certification in Digital Marketing Online Programme (Batch-22)

Now at just ₹ 20999 ₹ 3599942% off

Advance Graphic Designing Course (Batch-9) : 90 Hours of Learning
Advance Graphic Designing Course (Batch-9) : 90 Hours of Learning

Now at just ₹ 15999 ₹ 3599956% off

Flipkart Hot Selling Course in 2024
Flipkart Hot Selling Course in 2024

Now at just ₹ 10000 ₹ 3000067% off

Advanced Certification in Digital Marketing Classroom Programme (Batch-3)
Advanced Certification in Digital Marketing Classroom Programme (Batch-3)

Now at just ₹ 29999 ₹ 9999970% off

Basic Digital Marketing Course (Batch-24): 50 Hours Live+ Recorded Classes!
Basic Digital Marketing Course (Batch-24): 50 Hours Live+ Recorded Classes!

Now at just ₹ 1499 ₹ 999985% off

WhatsApp Business Marketing Course
WhatsApp Business Marketing Course

Now at just ₹ 599 ₹ 159963% off

Advance Excel Course
Advance Excel Course

Now at just ₹ 2499 ₹ 800069% off