Replacement for PHP's __autoload function?

后端 未结 6 1833
攒了一身酷
攒了一身酷 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';
      }
    }
    

提交回复
热议问题