doufan9805 2010-07-16 00:59
浏览 23

PHP __autoload()可以来自同一个类的另一个__autoload()中的一个类吗?

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.

  • 写回答

2条回答 默认 最新

  • dtrn74832 2010-07-16 01:16
    关注

    __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...

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据