Exploring Magic Methods in PHP 5


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


__construct()
This methods gets called whenever an object of a class is instantiated. This method is a part of Object Oriented Programming concept in PHP 5. To know more about this methods refer PHP 5 Tutorial – __construct Method.

__destruct()
This methods gets called whenever an object of a class is destroyed or object goes out of scope. This method is a part of Object Oriented Programming concept in PHP 5. To know more about this methods refer PHP 5 Tutorial – __destruct Method.

__set()
This methods get automatically called whenever you assigns data to a undefined attributes of an class in PHP 5. To know more about this method refer PHP 5 Tutorial – __set() magic method.

__get()
This methods get automatically called when you try to retrieves the data of undefined attributes of an class in PHP 5. To know more about this method refer PHP 5 Tutorial – __get() magic method.

__call()
This methods get automatically called you call and undefined methods of an class in PHP 5. To know more about this method refer PHP 5 Tutorial – __call() magic method.

__toString()
This method is called whenever an class object is treated as string and is echo or print(). This methods is very useful if you want to check the object methods and attributes. To know more on this method refer PHP 5 Tutorial – __toString magic method.

__sleep()
This methods gets called when you serialize the object in PHP 5. With this method call we can define the way how the data object will be stored. To know more on this method refer PHP 5 Tutorial – __sleep magic method.

__wakeup()
This methods gets called when the object is about to be unserialized in PHP 5. With this method call we can do necessary initial operation before starting operation on the received data object. To know more on this method refer PHP 5 Tutorial – __wakeup magic method.

__isset()
This methods get automatically called whenever you try to check the existence of the undeclared attributes of the class using isset function of PHP. To know more about this method refer PHP 5 Tutorial – __isset() magic method.

__unset()
This methods get automatically called whenever you try to check the destroy or clear an undeclared attributes of the class using unset function of PHP. To know more about this method refer PHP 5 Tutorial – __unset() magic method.

__autoload()
This methods get automatically called whenever you try to load an object of class which resides in separate file and you have not included those files using include,require and include_once. To use this method it is mandatory to the PHP filename as that of the class name because this methods accepts the class name as the argument. To know more about this method refer PHP 5 Tutorial – __autoload() magic method.

__clone()
PHP5 has introduced clone method which creates an duplicate copy of the object. __clone methods automatically get called whenever you try to call clone methods in PHP 5. This operator does not creates a reference copy. To know more about this method refer PHP5 Tutorial – __clone() magic method.

Tagged , , . Bookmark the permalink.

5 Responses to Exploring Magic Methods in PHP 5

  1. Newbie says:

    This is an excellent PHP reference. Thanks alot for the pointers.

  2. manoilayans says:

    Hello Hitesh,

    Nice collection, Can we say the magic methods are used as Exception handles like catch and try related logic..

    Pls clarify .. :)

  3. Faruq Shaik says:

    Very usefull article. thanks for sharing to all.

  4. Salim says:

    Made my day, This is excellent

  5. Nitesh says:

    Great one – very helpful post!

    This is also a very good reference on magic methods in PHP :

    http://www.programmerinterview.com/index.php/php-questions/php-what-are-magic-functions/

Leave a Reply