Replacement for PHP's __autoload function?

后端 未结 6 1873
攒了一身酷
攒了一身酷 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: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;
    

提交回复
热议问题