views:

54

answers:

2

OK, this is a tough one... I think, and I have a feeling the answer is simply no, but in that case I would like some answers for alternatives.

I have a very complex __autoload() function in a framework which can dynamically create classes. There a three classes required in order for the dynamic creation of a class called AuthActions -- these three are: fRecordSet, RecordSet, and AuthAction (note there's no S on that one).

My autoloader will look for a static method "init" on any class it loads and try to run that. On my ActiveRecord class it attempts to use AuthActions in order to get a list of supported actions on a particular active record. AuthAction (no S) also calls AuthActions within it's init, so basically the Active Record is loaded, and tries to load AuthActions, triggering the loading of the other three, and then when it finishes loading AuthAction still within the original autoloader AuthAction tries to call AuthActions which triggers another autoload since the original one did not complete yet.

This causes the following which has some echo statements to clarify:

Attempting to load ActiveRecord
Attempting to load fActiveRecord
fActiveRecord loaded via /var/www/dotink.org/inkwelldemo/inc/lib/flourish
ActiveRecord loaded via /var/www/dotink.org/inkwelldemo/inc/lib
Attempting to load AuthActions
Attempting to load RecordSet
Attempting to load fRecordSet
fRecordSet loaded via /var/www/dotink.org/inkwelldemo/inc/lib/flourish
RecordSet loaded via /var/www/dotink.org/inkwelldemo/inc/lib
Attempting to load AuthAction
AuthAction loaded via /var/www/dotink.org/inkwelldemo/models

Fatal error: Class 'AuthActions' not found in /var/www/dotink.org/inkwelldemo/models/AuthAction.php on line 24

The problem here is that the subsequent call to __autoload('AuthActions') would succeed because the three classes it requires are now in place... but it seems to die on the premise alone that it's already trying to autoload 'AuthActions' -- this seems to be hardwrit into PHP.

In testing this I found the following will loop forever with no error:

function __autoload($class) {
  __autoload($class);
}

$foo = new Bar();

While this one below will error similarly:

function __autoload($class) {
  $test = new Bar();
}

$foo = new Bar();

This behavior seems inconsistent as essentially they should amount to the same thing (kinda). If PHP internally triggered autoloads acted like a user call to __autoload() I don't think I would have a problem (or if I did the then it would be a problem of it looping forever which would be a separate issue of determining why the class wasn't getting loaded to resolve the dependencies).

In short, I need a way to either recursively loop the autoloader like this based on PHP triggered autoloads... is this simply not possible? Is it a bug in my particular version possibly? It seems to be affecting 5.2.6 - 5.3.2 in my tests, so I can't imagine it's a bug overall.

Update:

The code for the init method() on AuthAction is below:

        /**
     * Initializes the AuthAction model
     *
     * @param array $config The configuration array
     * @return void
     */
    static public function init($config) {

        // Establish permission definitions and supported actions

        $every_permission  = 0;
        $supported_actions = array();

        foreach (AuthActions::build() as $auth_action) {
            $action_name         = $auth_action->getName();
            $action_value        = intval($auth_action->getBitValue());
            $every_permission    = $every_permission | $action_value;
            $supported_actions[] = $action_name;
            define(self::makeDefinition($action_name), $action_value);
        }
        define('PERM_ALL', $every_permission);

    }

You can see where it is calling AuthActions as a separate class, and mind you it is only because it is being loaded within the original attempt to load that it fails. I can obviously remove this code and it will work -- the original load of AuthActions will complete successfully as all the pre-requisite classes are then loaded.

That said, init() is the most appropriate place for this code, and even if I am unable to use inter-dependent classes which haven't been loaded yet within init() I would still prefer to keep that functionality. Any alternative way of implementing that functionality would be great... Ideally PHP would have events for such things where you could for example register a callback when say an "autoloaded" event is triggered. This would allow the code to run AFTER the original autoload and would resolve this seemingly meaningless restriction.

Now with that said, any suggestions how to automatically call init() on a class every time it is loaded are appreciated and welcome.

+1  A: 

__autoload method only gets called when you are are trying to use a class/interface which hasn't been defined yet.

Therefore with your example

function __autoload($class) {
  $test = new Bar();
}

$foo = new Bar();

You are attempting to load the class bar which has not been required/requiered_once therefore the class is not defined so it calls the __autoload method as a last resort then within the autoload you are again trying to load the same class which has not been defined.

The correct way to use the __autoload would be as shown in the php site.

<?php
function __autoload($class_name) {
    require_once $class_name . '.php';
}

$obj  = new MyClass1();
$obj2 = new MyClass2(); 
?>

And all your init stuff can be put in __constructor can't it?

unless i'm missing something from your question...

ozatomic
You might want to check out the SPL implementations for __autoload to get a better idea of the how it is intended to be used. You can do some pretty slick file loading if you follow the Zend style class/directory structure and namespacing or using 5.3 and the namespace mechanic.http://us2.php.net/manual/en/function.spl-autoload.php
tsgrasser
SPL doesn't do anything more than create a stack of autoloaders from what I'm aware of. Mine does something quite different, but regardless,the problem at hand is not resolved by SPL autoloaders as each autoloader will still be treated with the same abrupt death on any such attempts to load the same class being currently loaded.
Gent
Might I also add to the original poster ozatomic, the examples I provided were just to show how the behavior is different depending on whether autoload is triggered internally from whether it's called by the user. It only relates to my problem insofar as I need it to run a second time. I am not simply trying to load the same class forever, I realize that is not how autoloader is used.
Gent
+1  A: 

I think this

My autoloader will look for a static method "init" on any class it loads and try to run that.

is the key to your problem. Your autoloader shouldn't be running anything, it should be including (or otherwise defining) the class and nothing else.

What sort of code are you trying to run when a class is defined (ie. in your static "init" method)?

Brenton Alker
Brenton, I have edited the original question to include the init code and a few more paragraphs to clarify what I am looking for in my solution.I appreciate your point that autoloader shouldn't be running anything -- however, I can think of no other way to concisely ensure that a method is called on a class once it is loaded.
Gent