pabz
pabz

Reputation: 604

How to call __autoload() in php code

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?

Upvotes: 13

Views: 27983

Answers (5)

Mohit Mehta
Mohit Mehta

Reputation: 1285

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.

Upvotes: 9

PHMJ
PHMJ

Reputation: 11

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; 


?>

Upvotes: 1

RN Kushwaha
RN Kushwaha

Reputation: 2136

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';
    });

    ?>

Upvotes: 0

Azimi
Azimi

Reputation: 9667

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!");
   }
}

Upvotes: 3

alex
alex

Reputation: 490607

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.

Upvotes: 18

Related Questions