PHP OOPs Concepts

Magic Methods

Written by Anuj kumar

Some predefined function by php compiler which executes on some event is called magic methods. Magic methods starts with prefix __, for example __call, __get, __set. There are diffrent type of magic methods in php. List given below-

List of List of Magic Methods in PHP
Magic Method Description
__construct __construct method is called when we create object of our class. Basically this is used for creating constructor in php5.
__destruct __destruct method is called when object of our class is unset. This is just opposite of __construct.
__get __get is called when our object attempt to read property or variable of the class which is inaccessible or unavailable.
__set __set is called when object of our class attempts to set value of the property which is really inaccessible or unavailable in our class.
__isset __isset methods trigger when isset() function is applied on any property of the class which isinaccessible or unavailable.
__unset __unset is something opposite of isset method. This method triggers when unset() function called on inaccessible or unavailable property of the class.
__call __call magic method trigger when we are attempting to call method or function of the class which is either inaccessible or unavailable.
__callstatic __callstatic execture when inaccessible or unavailable method is in static context.
__sleep __sleep methods trigger when we are going to serialize your class object.
__wakeup __wakeup executes when we are un serializing any class object.
__toString __toString executes when you are using echo on your object.
__invoke __invoke called when we are using object of your class as function

Above magic methods of php executes on some specif events occur on your class object. For example if you simply echo your object then __toString method trigger.
Let us create group of related magic method and analyze how it is working
__construct and  __destruct magic method in PHP
__construct method trigger on creation of object. And  __destruct triggers of deletion of object.

Simple example of  __construct and  __destruct magic method
Basic  example of __get , __set , __call and __callStatic magic methods
Basic example of __isset and __unset magic method in php

About the author

Anuj kumar

I'm Anuj kumar a web developer with 4+ year experience .I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to is provide php , jquery , mysql , phpoops and other web development tutorials. I am trying best effort to make PHPGurukul useful for every single moment spend on this website. If you think this website is useful to visit please share with your friend and buddies.