PHP OOP Magic Methods

What are the PHP Magic Methods & How to use them?

PHP magic methods are special types of methods that will run automatically when certain actions are performed on an Object of a Class.

You only define magic methods, you don’t need to call these methods.

List of the PHP magic methods

One thing you can note is that PHP magic methods always start with a double underscore __.

  • __construct() – The constructor method runs automatically as soon as an object of the class is created.
  • __destruct() – The destructor method runs after all the work of an object is finished.
  • __call($fun, $arg)
  • __callStatic($fun, $arg)
  • __get($property)
  • __set($property, $value)
  • __isset($content)
  • __unset($content)
  • __sleep()
  • __wakeup()
  • __toString()
  • __invoke()
  • __set_state($array)
  • __clone()
  • __debugInfo()