The auto load function allowed you to do that little bit extra to prevent file inclusion errors.

and auto load function is executed under the following conditions

  • A file that you attempted to include does not exists
  • a class you attempt to initiate does not exists in global scope.


that might not seem helpfull but lets take a look at this example:

Directory Structure
Code: 
index.php
-- includes /
---- database.class.php
---- profile.class.php
Now you will see the consistency above where within the includes folder we have several files disigned like so (<name[lower,no underscores]>.class.php), this needs to be the same layout for every file within that directory.

Now the traditional method of loading one of those files is like so:
PHP Code: 
include 'includes/database.class.php';
$Database = new Database(); 
but lets say loading the database is conditional! meaning that depending on certain factors the database may not be loaded for a perticular user! but you would usually still include it at the head of your script right.

now lets take a look at a little autoloader function.

PHP Code: 
function __autoload($class)
{
    
$file 'includes/' strtolower($class) . '.class.php';
    if(
file_exists($file))
    {
        require_once 
$file;
    }

Simple right, you can understand whats going on here, but what does this do? take a look at how we now initiate classes.

PHP Code: 
include 'functions/autoload.php'//Load the above function.

$Database = new Database();
$Profile = new Profile(); 
aslong as the class names are named the same as the first segment of the file name, (case-insensitive) this works like a charm.

heres an example of Litewarez

Filename: includes/litewarez.class.php
Classname: class Litewarez{

So you can now use AutoLoaders

Happy Coding
litewarez Reviewed by litewarez on . Using the __autoload feature in PHP The auto load function allowed you to do that little bit extra to prevent file inclusion errors. and auto load function is executed under the following conditions A file that you attempted to include does not exists a class you attempt to initiate does not exists in global scope. that might not seem helpfull but lets take a look at this example: Rating: 5