The autoload function I am using is as follows:-
function __autoload($moduleName)
{
//Logic to check file existence- include if exists else redirect to fallback page
}
Does it not take any other arguments? I want to perform some logic based on some variables inside the autoload function. How do I do it without the use of global variables?
Thanks
Additional Details
I think this is not possible inside __autoload() class but still trying to explain with an example.
I have a modules.config file which has an array:-
$viewClassMap = array('search_classes' => 'commonClassListings',
'search_packs' => 'commonPackListings',
);
The above array means that class commonClassListings has to be included in case of search_classes view, class commonPackListings has to be included in case of search_packs view. For all other views, by default, commonDisplay class will be included
function __autoload($viewName,$viewClassMap)
{
if(in_array($viewName,$viewClassMap))
{
$viewTobeIncluded = $viewClassMap[$viewName];
include path/to/$viewTobeIncluded;
}
else
{
include path/to/commonDisplay;
}
}
Now, I think the logic inside __autoload function has to be moved out, and first the view to be loaded has to be computed and then only autoloading has to be called. That is the purpose of autoloading also (to include the class file whose object was initiated).
Updates
My autoload function is not inside the view class whose object I am going to initiate at run time (Unlike in Blizz's example). I have a common autoload function defined in a pageLoader.php file which is included in the common froncontroller. For every module, the view class is initatiated like
$view = new search_classes();
Then the common autoload function has to check the corresponding parent view class (commonClassListings in this case) is present or not, if so include it and the search_classes view itself and iniate the object, else fallback. For this I need to pass the $viewClassMap
array to the autoload function. Is that possible?