Magic methods are Php predefined functions, with special names and will be triggered in response to particular PHP events. Magic methods starts with prefix __. PHP reserves all function names starting with __ as “magical”.

It is recommended that you do not use function names with __ in Php unless you want some documented magic functionality.


List of Magic Methods in PHP

__callstatic execture when inaccessible or unavailable method is in static context.

Magic Method Description
__construct This magic methods is called when someone create object of your class. Usually this is used for creating constructor in php5.
__destruct This magic method is called when object of your class is unset. This is just opposite of __construct.
__get This method called when your object attempt to read property or variable of the class which is inaccessible or unavailable.
__set This method called when object of your class attempts to set value of the property which is really inaccessible or unavailable in your class.
__isset This magic 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 whenunset() function called on inaccessible or unavailable property of the class.
__call __call magic method trigger when you are attempting to call method or function of the class which is either inaccessible or unavailable.
__sleep __sleep methods trigger when you are going to serialize your class object.
__wakeup __wakeup executes when you are un serializing any class object.
__toString __toString executes when you are using echo on your object.
__invoke __invoke called when you are using object of your class as function


You cannot throw an exception from within a __toString() method. Doing so will result in a fatal error.
Note: Before PHP 5.2.0 the __toString() method was only called when it was directly combined with echo or print. Since PHP 5.2.0, it is called in any string context. Converting objects to string would cause E_RECOVERABLE_ERROR, if it does not contain __toString() method.



There are eight magical constants. These special constants are case-insensitive and are as follows:

Magic Constant Description
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned. (Added in version 4.0.2)
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent todirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in version 5.3.0)
__FUNCTION__ The function name. (Added in version 4.3.0)
__CLASS__ The class name. The class name includes the namespace it was declared in (e.g. Foo\\Bar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.  (Added in version 4.3.0)
__TRAIT__ The trait name. The trait name includes the namespace it was declared in (e.g. Foo\\Bar). (Added in version 5.4.0)
__METHOD__ The class method name. (Added in version 5.0.0)
__NAMESPACE__ The name of the current namespace. (Added in version 5.3.0)
Note: __DIR__ is actually equivalent to realpath(dirname(__FILE__))
Note: __FUNCTION__ returns only the name of the functionwhile as __METHOD__ returns the name of the class alongwith the name of the function

Leave a Reply