Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Register Your Own Autoload Functions

DZone's Guide to

Register Your Own Autoload Functions

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

PHP has a few built-in magic methods: __get(), __set(), __sleep(), __toString(), etc. These functions are all really useful, but they are not used very often in your day-to-day development. But one magic method is really useful: the __autoload() function.

This function is automatically called each time you try to instantiate a class but the class can't be found.

When you normally call a class to use in your code you will do something like this:

// include the file for the class
include_once 'MyClass.php';
include_once 'OtherClass.php';

// instantiate the class
$class = new MyClass();
$class2 = new OtherClass();

This is fine if you're just using one class, but there could be times in your script when you have to reference lots of different classes. This is when using an autoload function is perfect.

The __autoload function will be called when you instantiate the class and will pass in the class name as the parameter to the function. If you are using good PHP coding practices, the file should be called the same as your class name, or be prefixed with something. This means that you will be able to find the file you need from just the class name.

<?php
function __autoload($classname) {
    $filename = $classname . ".php";

    if(file_exists( $filename ))
    {
        include_once($filename);
    }
}

$class = new MyClass();
$class2 = new OtherClass();
?>

The problem with this function is that it can only be defined once in your application, so if you, like many others, store your classes in a neat folder structure, you will need to come up with a naming structure to use this function. For example, you could prefix the new class with the folder structure, so if the class is inside a folder you will name the class FolderOne_FolderTwo_MyClass(). Then, in your autoload function, you can explode the class name on the underscores and work out the file structure from the class name.

But this isn't very flexible and your class names could become huge. To solve this problem there is another function that you can use to register your own autoload functions. Using spl_autoload_register() can define your own function that can be used just like the __autoload() function. The benefit of this is that it will allow you to define multiple autoload functions. This allows you to create an autoload function for each of your namespaces, if needed.

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

spl_autoload_register('my_own_autoloader');

function another_autoloader($class) {
    include 'controllers/' . $class . '.php';
}

spl_autoload_register('another_autoloader');

Take a look at the Indigo.Design sample applications to learn more about how apps are created with design to code software.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}