dongzi0602 2013-02-14 03:40
浏览 31
已采纳

PHP中的自动加载助手

I currently have a manual method for registering helpers into my base connection class which goes pretty much as follows:

class db_con
{
    // define the usual suspect properties..

    public $helpers; // helper objects will get registered here..

    public function __construct()
    {
        // fire up the connection or die trying

        $this->helpers = (object) array();
    }

    public function __destruct()
    {
        $this->helpers = null;
        $this->connection = null;
    }

    // $name = desired handle for the helper
    // $helper = name of class to be registered
    public function register_helper($name, $helper)
    {
        if(!isset($this->helpers->$name, $helper))
        {
            // tack on a helper..
            $this->helpers->$name = new $helper($this);
        }
    }

    // generic DB interaction methods follow..
}

Then a helper class such as..

class user_auth
{
    public function __construct($connection){ }

    public function __destruct(){ }

    public function user_method($somevars)
    {
        // do something with user details
    }
}

So after creating the $connection object, i would then manually register a helper like so:

$connection->register_helper('users', 'user_auth');

Now my question is, could I somehow autoload helper classes inside the base connection class? (within the register_helper() method or similar) Or am I limited to loading them manually or via an external autoloader of some form?

My apologies if this question has been answered elsewhere, but I just haven't found it (not for lack of trying) and I haven't any real experience autoloading anything yet.

Any help or pointers greatly appreciated, thanks in advance! :)

EDIT: As per Vic's suggestion this is the working solution I came up with for the register method..

public function register_handlers()
{
    $handler_dir = 'path/to/database/handlers/';
    foreach (glob($handler_dir . '*.class.php') as $handler_file)
    {
        $handler_bits = explode('.', basename($handler_file));
        $handler = $handler_bits[0];
        if(!class_exists($handler, false))
        {
            include_once $handler_file;

            if(!isset($this->handle->$handler, $handler))
            {
                $this->handle->$handler = new $handler($this);
            }
        }
    }
}

This appears to include and register the objects absolutely fine for now, whether this solution is a "good" one or not, I can't know without more input or testing.

  • 写回答

1条回答 默认 最新

  • dougan6402 2013-02-14 09:59
    关注

    The code could look something like below, but why would you need this?

     public function register_helper($name, $helper)
     {
          if(!isset($this->helpers->$name, $helper))
          {
               $this->load_class($helper);
               // tack on a helper..
               $this->helpers->$name = new $helper($this);
          }
     }
    
     private function load_class($class) 
     {
         if( !class_exists($class, false) ) {
              $class_file = PATH_SOME_WHERE . $class . '.php';
              require $class_file;
         }
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)