what are magic methods in php and why we use them

Magic methods in php have their importance and they are really important and they are start with double underscore(__).

 

 

 

they are listed below:

  1. __autoload()
  2. __clone()
  3. __constuctor()
  4. __call()
  5. __destruct()
  6. __get()
  7. __isset()
  8. __set()
  9. __sleep()
  10. __tostring()
  11. __unset()
  12. __wakeup()

they are built in php functions and we can use them anywhere in class,method , function or anywhere else.

now question comes to our mind why we use them the answer is simple as that we do not need to go hard coding for them ; actuall

they are made for development ease as __isset(),__set(),__unset() and __get() are used for restoring ,determining and inspecting the elements in class or database etc.

And more about __sleep() is used to return to us the array  of all variables that need to be saved and while __wakeup gets all variables of an array.