I have this autoload code :
function __autoload($class_name)
{
//class directories
$directorys = array(
'./Controls/',
'./Config/',
'./Utility/'
);
//for each directory
foreach($directorys as $directory)
{
//see if the file exsists
if(file_exists($directory.$class_name . '.php'))
{
require_once($directory.$class_name . '.php');
//only require the class once, so quit after to save effort (if you got more, then name them something else
return;
}
}
}
I have three directories that they're holding all of my classes and functions.
Can I create a single autoload file in Controls
directory and use it to load all function or classes in other php files I mean for example index.php
file in /portal/main/index.php
is it possible to load classes that are controls
and config
in index.php file without including any file in above of index.php
file
I mean autoload automatically understands which file is requesting a class or function and include that file for it.
updated code :
function __autoload($class_name)
{
//class directories
$directorys = array(
'/Controls/',
'/Config/',
'/Utility/'
);
//for each directory
$ds = "/"; //Directory Seperator
$dir = dirname(__DIR__); //Get Current file path
$windir = "\\"; //Windows Directory Seperator
$path = str_replace($windir, $ds, $dir);
foreach($directorys as $directory)
{
//see if the file exsists
if(file_exists( $path . $directory . $class_name . '.php'))
{
require_once( $path . $directory . $class_name . '.php');
//only require the class once, so quit after to save effort (if you got more, then name them something else
return;
}
}
}
I have updated the code and it include files, but the only problem that i have is that this function is not running automatically,
for example my autoload file is in : root/Controls/autoload.php
and I need some classes and functions in :root/portal/index.php
when i define classes in index.php i get error that the file is not exists
and i should manually call autoload.php
file in index.php
how can i make autoload smart which i shouldn't have include it in each file for including classes ?
please help me out. thanks in advance