PHP 5 Tutorial - __autoload Magic Method
This tutorial will guide you through the __autoload() Magic Method. The __autoload() magic method of PHP5 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. It is recommended to use the filename as that of the class name.
Syntax:
< ? function __unset($classname) { require($classname.".php"); } //$classname is the name of the Class. ?>
Example - Basic AutoLoad Magic Method Usage
< ? function __autoload($classname) { include $classname.".php"; //Here $classname=magicmethod1 } $a = new magicmethod1(); ?>
Explanation for the Example:
Here i am trying to create an object of magicmethod1 class, but i have not included the magicmethod1.php so PHP compiler calls the __autoload() method which include that magicmethod1.php file.
Code for magicmethod1.php
< ? class magicmethod1 { function __construct() { echo "MagicMethod1 Class Called"; } } ?>
Output: MagicMethod1 Class Called
Similar Posts
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


Can you put up a tutorial on method overloading. If you’ve already explained in one of your tutorials please direct me to it.
thanks.