views:

79

answers:

4

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?

+3  A: 

The only way I can think of is registering a class object as autoload handler (with spl_autoload_register). You can register an instance at any time by specifying array($oInstance, "functionName") as the callback.

You would be able to set variables on that class instance before you do the operation that will trigger the auto load.

Edit: As per your request a small example on how this works. Note that I usually work with static methods in a class for the autoload so I don't know if it works with an instance, but I checked the documentation and it should. You can just make the function and the array static if it doesn't work

class Test
{
    public $viewClassMap =  array('search_classes' => 'commonClassListings',
                       'search_packs' => 'commonPackListings',
                       );

   public function autoload($viewName)
   {
    if (in_array($viewName,$this->viewClassMap))
            ...
    }
}

$test = new Test;
spl_autoload_register(array($test, 'autoload'));
Blizz
Hi Blizz, if you could explain that with an example. I am not sure if that solves my problem. Please check the additional details section in the question
sandeepan
There you go. answer updated.
Blizz
Hi BLizz thanks for reply, I will check the same...
sandeepan
Please read my updates. Thanks
sandeepan
A: 

the autoloader's job is simply to "include" the appropriate file containing the class to be invoked. the constructor of the autoloaded class can have arguments to match the arguments of the instantiator.

-- xyz.php --
class xyz {
  public function __construct($a,$b) {
    // whatever u need to do with $a and $b
  }
}
-- caller.php --
new xyz(123,'abc'); // <-- calls autoloader of xyz then passes 123 to $a and 'abc' to $b
stillstanding
A: 

You do not call the __autoload function so passing parameters to it doesn't make sense. You can use static variables if you want to preserve some state in it (e.g. tracking all loaded classes / timings or whatever).

Can you tell us more about the logic you want to do? May be there are better ways.

FractalizeR
A: 

You do not call the __autoload function so passing parameters to it doesn't make sense. You can use static variables if you want to preserve some state in it (e.g. tracking all loaded classes / timings or whatever).

Can you tell us more about the logic you want to do? May be there are better ways.

FractalizeR
I do not call the autoload function. it is called automatically whenever a class object initiation is taking place. That's why the name autoload. Please check my Updates
sandeepan