dqroc48068 2013-07-12 19:29
浏览 23
已采纳

如何在自动加载器上加载Zend 2类

Previously, i am using zend 1.x. I am able to autoload zend classes using the below code.

// autoload class from Zend Lib
require_once ABSPATH.'/classes/Zend/Loader/Autoloader.php'; 
$loader = Zend_Loader_Autoloader::getInstance();    
 try{
// database connection      
$dbo = Zend_Db::factory('pdo_mysql', array( 
        'host'     => DB_HOST, 
        'username' => DB_USER, 
        'password' => DB_PW, 
        'dbname'   => DB_PREFIX.DB_NAME
    )); 
$dbo->getConnection();
// save database adapter for easy usage in other classes
Zend_Db_Table::setDefaultAdapter($dbo);
Zend_Registry::set('db', $dbo);

}catch(Zend_Db_Adapter_Exception $e){
print $e;
 //header("Location: http://www.google.com/error/");
}

I am upgrading to zend 2 as the classes might be better. May i know how do i autoload them?

  • 写回答

3条回答 默认 最新

  • dourui9570 2013-07-13 17:57
    关注

    If you are just using ZF2 as a standalone library without employing the full MVC framework, then autoloading is fairly straightforward:

    1. Make sure that the Zend directory is on your php include_path.
    2. Push an autoloader using spl_autoload_register

    The following is essentially what Zend\Loader\StandardAutoloader::loadClass() does when functioning as a fallback autloader:

    spl_autoload_register(function($class) {
        $f = stream_resolve_include_path(str_replace('\\', '/', $class) . '.php');
        if ($f !== false) {
            return include $f;
        }
        return false;
    });
    

    This would use the PSR-1 autloading mechanism for all classes, not just the Zend classes.

    Alternatively, you could just do the following:

    require_once 'Zend/Loader/StandardAutoloader.php';
    $autoloader = new Zend\Loader\StandardAutoloader(array(
        'fallback_autoloader' => true,
    ));
    $autoloader->register();
    

    As above, this will apply PSR-1 autoloading to all classes. If you want this mechanism for the Zend classes only, then pass 'fallback_autoloader' => false.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata