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 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表