59

what is autoload in PHP?

hakre
  • 193,403
  • 52
  • 435
  • 836
DEVOPS
  • 18,190
  • 34
  • 95
  • 118

4 Answers4

41

This will be of help to you about usage of autoload. http://ditio.net/2008/11/13/php-autoload-best-practices/

It's a magic function that helps you include / require files using class name.

function __autoload($class_name) 
{
    require_once $DOCUMENT_ROOT . “/classes/” . $class_name .“.php”;
}

It's deprecated at PHP 7.2.0 and spl_autoload_register is recommended for that purpose.

osm
  • 4,186
  • 3
  • 23
  • 24
  • 4
    The [_autoload](http://php.net/manual/en/function.autoload.php) function as advised in this article should not be used anymore, the [spl_autoload_register](http://php.net/manual/en/function.spl-autoload-register.php) function is preferred. – Félix Adriyel Gagnon-Grenier Feb 25 '17 at 05:39
  • 11
    This link could break, please place the most vital parts of the link inside the answer. – Goose May 15 '17 at 14:29
40

What is autoloading?

Every time you want to use a new class in your PHP project, first you need to include this class (using include or require language construct, that’s right this are not functions). However if you have __autoload function defined, inclusion will handle itself.

include "classes/class.Foo.php";
 
$foo = new Foo;
$foo->start();
$foo->stop();

Basic Autoloading Example

function __autoload($class_name) 
{
    require_once $DOCUMENT_ROOT."classes/class.".$class_name.".php";
}
 
$foo = new Foo;
$foo->start();
$foo->stop();

PHP Official

Other

Update

PHP 5 introduced the magic function __autoload() which is automatically called when your code references a class or interface that hasn’t been loaded yet.

The major drawback to the __autoload() function is that you can only provide one autoloader with it. PHP 5.1.2 introduced spl_autoload() which allows you to register multiple autoloader functions, and in the future the __autoload() function will be deprecated.

The introduction of spl_autoload_register() gave programmers the ability to create an autoload chain, a series of functions that can be called to try and load a class or interface. For example:

<?php
function autoloadModel($className) {
    $filename = "models/" . $className . ".php";
    if (is_readable($filename)) {
        require $filename;
    }
}

function autoloadController($className) {
    $filename = "controllers/" . $className . ".php";
    if (is_readable($filename)) {
        require $filename;
    }
}

spl_autoload_register("autoloadModel");
spl_autoload_register("autoloadController");
cyclaminist
  • 1,697
  • 1
  • 6
  • 12
Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130
  • 2
    Please don't use `__autoload()`. It's [better to use spl_autoload_register](http://stackoverflow.com/questions/7651509/someone-explain-spl-autoload-autoload-and-spl-autoload-register) – Machavity Jun 17 '16 at 15:21
30

Here is the official documentation: https://www.php.net/manual/en/language.oop5.autoload.php

In short, it just allows you to define search paths for classes so you wouldn't be required to include the files containing them manually.

I suggest you should develop a habit of searching php.net by just appending function names or obvious keywords to the address. That's how I found php.net/autoload. It's quite convenient like that.

Liam
  • 19,819
  • 24
  • 83
  • 123
slikts
  • 8,020
  • 1
  • 27
  • 47
1
an __autoload() 

//function which is automatically called in case you are trying to use     
//a class/interface which hasn't been defined yet.

function __autoload($class_name) {
    include $class_name . '.php';
}
Unheilig
  • 16,196
  • 193
  • 68
  • 98
kumardippu
  • 599
  • 4
  • 11