The PHP reserves some functions which can be called when needed in within a class. These predefined functions are called magic methods in PHP classes.

Magic methods are described as listed here:

  • _construct() - This method can be used before initialization of an object. It allows to declare constructor methods for classes.
  • _destruct() -Just like constructor, a destructor would have to explicitly call the parent in the destruct body.
  • _call() - It is called when invoking inaccessible methods in an object context.
  • _callStatic() - It is called when invoking inaccessible methods in a static context.
  • _set() - It is used when writing data to inaccessible properties.
  • _get() - It is used when reading data from an inaccessible properties.
  • _isset() - It is used when calling an inaccessible properties.
  • _unset() - It is used when invoking an inaccessible properties.
  • _sleep() - This function is used with a large object which do not need to be saved completely.
  • _wakeup() - It is used to reestablish a database connection.
  • _toString() - It allows the class to decide how to react when method is treated like a string.
  • _invoke() - This method is used when an object is treated as a function.
  • _set_state() - This method is used to export an array as an argument of the function.
  • _clone() - It creates a copy of an object to replicate its properties.
  • _debuginfo() - This function is used for debugging the variable of an object.

 

›› go to examples ››