Marvin Mustafa
Marvin Mustafa

Reputation: 163

autoloading multiple files on multiple folder with php?

how to Autoload multiple files that may stored inside more than one folder ? what i already do is like demonstrated at the pic : enter image description here

creating a file required at Index.php [ init.php ] which contains :

<?php
spl_autoload_register(function ($class){
    require_once "classes/class.$class.php";
});

question no 1 : how to autoload another file which lives in another folder eg : Conf/class.Conf.php ? question no 2 : can i use another name convention for the other auto-loading process ?

it'll be better if you provide a coded example :)

Upvotes: 2

Views: 1597

Answers (1)

Marvin Mustafa
Marvin Mustafa

Reputation: 163

the Problem Solved as @dan-miller Comment mention checking for file existence then require it for each new folder

<?php
spl_autoload_register(function ($class){
    $filename="classes/$class.php";
        if(!file_exists($filename))
        {
            return "file : $filename is not Exist on the Given Path";
        }
    require_once "classes/$class.php";
});
spl_autoload_register(function ($class){
    $filename="conf/$class.php";
        if(!file_exists($filename))
        {
            return "file : $filename is not Exist on the Given Path";
        }
    require_once "conf/$class.php";
});

*** that's for testing Purpose

Upvotes: 2

Related Questions