Replacement for PHP's __autoload function?

后端 未结 6 1832
攒了一身酷
攒了一身酷 2021-01-05 08:36

I have read about dynamically loading your class files when needed in a function like this:

function __autoload($className)
{
   include(\"classes/$className         


        
相关标签:
6条回答
  • 2021-01-05 09:11

    Best you can do is to define your own object responsible for autoloading for any subsystem you are programming.

    For example:

    class BasketAutoloader
    {
      static public function register()
      {
        spl_autoload_register(array(new self, 'autoload'));
      }
    
      public function autoload($class)
      {
        require dirname(__FILE__).'/'.$class.'.php';
      }
    }
    
    0 讨论(0)
  • 2021-01-05 09:17

    I have used the following code to use spl_autoload_register in a way that it will degrade if it isn't present, and also handle libraries that use __autoload, that you need to include.

    //check to see if there is an existing __autoload function from another library
    if(!function_exists('__autoload')) {
        if(function_exists('spl_autoload_register')) {
            //we have SPL, so register the autoload function
            spl_autoload_register('my_autoload_function');      
        } else {
            //if there isn't, we don't need to worry about using the stack,
            //we can just register our own autoloader
            function __autoload($class_name) {
                my_autoload_function($class_name);
            }
        }
    
    } else {
        //ok, so there is an existing __autoload function, we need to use a stack
        //if SPL is installed, we can use spl_autoload_register,
        //if there isn't, then we can't do anything about it, and
        //will have to die
        if(function_exists('spl_autoload_register')) {
            //we have SPL, so register both the
            //original __autoload from the external app,
            //because the original will get overwritten by the stack,
            //plus our own
            spl_autoload_register('__autoload');
            spl_autoload_register('my_autoload_function');      
        } else {
            exit;
        }
    
    }
    

    So, that code will check for an existing __autoload function, and add it to the stack as well as your own (because spl_autoload_register will disable the normal __autoload behaviour).

    0 讨论(0)
  • 2021-01-05 09:21

    You can use spl_autoload_register(), adding any existing __autoload() magic to the stack.

    function my_autoload( $class ) {    
        include( $class . '.al.php' );
    }
    spl_autoload_register('my_autoload');
    
    if( function_exists('__autoload') ) {
        spl_autoload_register('__autoload');
    }                                                                                         
    
    $f = new Foo;
    
    0 讨论(0)
  • 2021-01-05 09:21

    You could use Zend_Loader. (Assuming Zend Framework is available for you...)

    0 讨论(0)
  • 2021-01-05 09:35

    The correct way would be to use spl_autoload_register. To save a __autoload function that some 3rd-party introduced you can put that function on the autoloader-stack as well:

    if (function_exists('__autoload')) {
        spl_autoload_register('__autoload');
    }
    
    0 讨论(0)
  • 2021-01-05 09:35

    Use spl_autoload_register instead of __autoload. This will allow you to add your autoload function into the stack.

    0 讨论(0)
提交回复
热议问题