13

I am new to php and I want to use php5's __autoload functionality in my code. I wrote below code in my index.php but I don't understand how and when I should call __autoload function.

function __autoload($class) {
    if (file_exists($class . '.php')) {
        include($class . '.php');
    } else {
        throw new Exception('Unable to load class named $class');
    }
}

I have seen this thread also but I do not have such autoloader class in my application. Do every application need a separate class in order to use autoloading? If not can I have a single function like above and complete it's task? Can anyone explain how to call above __autoload function inside my php code?

Community
  • 1
  • 1
pabz
  • 534
  • 1
  • 5
  • 15

5 Answers5

18

You don't call __autoload() yourself, PHP does when it is trying to find the implementation of a class.

For example...

function __autoload($className) {
     include $className . '.php';
}

$customClass = new CustomClass;

...this will call __autoload(), passing CustomClass as an argument. This (stupid for example's sake) __autoload() implementation will then attempt to include a file by tacking the php extension on the end.

As an aside, you should use spl_autoload_register() instead. You can then have multiple implementations, useful when using multiple libraries with auto loaders.

...using __autoload() is discouraged and may be deprecated or removed in the future.

Source.

alex
  • 479,566
  • 201
  • 878
  • 984
9

In PHP __autoload() is a magic method, means it gets called automatically when you try create an object of the class and if the PHP engine doesn't find the class in the script it'll try to call __autoload() magic method.

You can implement it as given below example:

function __autoload($ClassName)
{
    include($ClassName.".php");
}

$CustomClassObj = new CustomClass();
$CustomClassObj1 = new CustomClass1();
$CustomClassObj2 = new CustomClass2();

It'll automatically include all the class files when creating new object.

P.S. For this to work you'll have to give class file name same as the class name.

Tinsae
  • 577
  • 5
  • 10
Mohit Mehta
  • 1,283
  • 12
  • 21
3

You guys need to use require_once instead of include which will eat your memory The best way is:

function __autoload($class) {
   $class_name = strtolower($class);
   $path       = "{$class}.php";
   if (file_exists($path)) {
       require_once($path);
   } else {
       die("The file {$class}.php could not be found!");
   }
}
Amir Hassan Azimi
  • 9,180
  • 5
  • 32
  • 43
1

also you can use spl_autoload_register() for load php file and this is much better than __autoload for example :

<?php 
spl_autoload_register('myautoloadClass::myautoloadMethod');

class myautoloadClass{

   public static function myautoloadMethod(){
   require_once 'yourPath/'.'yourphpFile'.'.php';
   }

}

$obj = new xp; 


?>
PHMJ
  • 11
  • 1
0

Do not use __autoload() instead use spl_autoload_register. See php manual which says

spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future.

    <?php

    // function __autoload($class) {
    //     include 'classes/' . $class . '.class.php';
    // }

    function my_autoloader($class) {
        include 'classes/' . $class . '.class.php';
    }

    spl_autoload_register('my_autoloader');

    // Or, using an anonymous function as of PHP 5.3.0
    spl_autoload_register(function ($class) {
        include 'classes/' . $class . '.class.php';
    });

    ?>
RN Kushwaha
  • 2,081
  • 3
  • 29
  • 40
  • I think the word 'flexible' means that you can use your own implemented function as autoloader. – Amir Fo Nov 09 '18 at 13:53