Adam
Adam

Reputation: 20872

PHP Autoloader Function - still need require_once?

I'm getting my head around PHP autoloader. If I have the code below:

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

so long as my classes are named to fit the path above should I still need to use require_once

  require_once('scripts/classes/session.class.php');

Note: I'm including the __autoloading in the header() of the site (first page that is loaded. Do I need to include it on each page that is loaded to get its functionality to work? I would assume yes but I'm unsure...

thank you

Upvotes: 0

Views: 3040

Answers (3)

smarteist
smarteist

Reputation: 1421

You can include this file in start of script and then just set a directory level instead of this constant: WP_CONTENT_DIR This will automatically includes any file that it needs

<?php

class autoload
{

    private static function updatePhpFiles($dir_level, $php_files_json_name)
    {
        /**Get all files and directory using iterator.*/
        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir_level));
        $filePaths = array();

        /**Get all php files in this directory level.*/
        foreach ($iterator as $path) {
            if (is_string(strval($path)) and pathinfo($path, PATHINFO_EXTENSION) == 'php') {
                $filePaths[] = strval($path);
            }
        }

        /**Encode and save php files dir in a local json file */
        $fileOpen = fopen($dir_level . DIRECTORY_SEPARATOR . $php_files_json_name, 'w');
        fwrite($fileOpen, json_encode($filePaths));
        fclose($fileOpen);
    }

    private static function includeMatchingFiles($dir_level, $php_files_json_name, $class_file_name)
    {
        $files = json_decode(file_get_contents($dir_level . DIRECTORY_SEPARATOR . $php_files_json_name), true);
        $inc_is_done = false;

        /**Include matching files here.*/
        foreach ($files as $path) {
            if (stripos($path, $class_file_name)) {
                require_once $path;
                $inc_is_done = true;
            }
        }
        return $inc_is_done;
    }

    public static function include_system_files($dir_level, $class_name)
    {
        $php_files_json = 'phpfiles.json';
        $class_file_name = $class_name . '.php';

        /**Include required php files.*/
        if (is_file($dir_level . DIRECTORY_SEPARATOR . $php_files_json)) {

            if (self::includeMatchingFiles($dir_level,$php_files_json, $class_file_name)) {
                return true;
            } else {
                self::updatePhpFiles($dir_level, $php_files_json);
                return self::includeMatchingFiles($dir_level, $php_files_json, $class_file_name);
            }

        } else {

            self::updatePhpFiles($dir_level, $php_files_json);
            return self::includeMatchingFiles($dir_level, $php_files_json, $class_file_name);

        }

    }

}

/**
 * Register autoloader.
 */
spl_autoload_register(function ($className) {
    autoload::include_system_files(WP_CONTENT_DIR, $className);
});

Upvotes: 0

IMSoP
IMSoP

Reputation: 97648

You need to define __autoload, or call spl_autoload_register, on every page loaded in the browser. So if all your pages use the same "header", putting it in there is enough.

Once you've done that, you never need to use include or require anywhere else: just mention the class Session, and PHP will use your autoload function to find the definition if it doesn't know it yet.

Upvotes: 2

Shoe
Shoe

Reputation: 76240

No, you should use include(). The class will be included only once when it is not already loaded:

From the PHP manual:

You may define an __autoload() function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet.

And no, you can include the autoloader function just once in the entire application.

Also it is good practice to use spl_autoload_register() instead of __autoload().

Upvotes: 1

Related Questions