Magic Methods in PHP

With PHP 5 Object Oriented Programming seems to becoming a reality in PHP but we all know that in PHP a variable can take any form depending on the data passed to it. Also PHP automatically creates variable and assigns values to it even is the variables are not defined. But in Object Oriented Programming all the data members/methods needs to be defined. To solve some of these problems in OOPS environment magic methods have been introduced in PHP5.

NOTE ON MAGIC METHODS:
  • Magic methods are the members functions that is available to all the instance of class
  • Magic methods always starts with “__”. Eg. __construct
  • All magic methods needs to be declared as public
  • To use magic method they should be defined within the class or program scope
Various Magic Methods used in PHP 5 are:
  • __construct()
  • __destruct()
  • __set()
  • __get()
  • __call()
  • __toString()
  • __sleep()
  • __wakeup()
  • __isset()
  • __unset()
  • __autoload()
  • __clone()

No comments: