简体   繁体   English

找不到Zend_Db

[英]Zend_Db not found

use Zend\Db;

$db = Zend_Db::factory('Pdo_Mysql', array(
    'host'     => 'localhost',
    'username' => 'root',
    'password' => '',
    'dbname'   => 'test'
));

I'am trying to require Zend_Db by the keyword 'use', but i get An error that there are not class Zend_Db, what i am doing wrong here? 我试图通过关键字'use'要求Zend_Db,但是我得到一个错误,提示没有类Zend_Db,在这里我在做什么错?

If you are using ZF1, then the class is Zend_Db . 如果使用的是ZF1,则该类为Zend_Db In this case, if you really want to use PHP 5.3 namespaces, then the correct usage is: 在这种情况下,如果您确实要使用PHP 5.3名称空间,则正确的用法是:

use \Zend_Db;

$db = Zend_Db::factory('Pdo_Mysql', array(
    'host'     => 'localhost',
    'username' => 'root',
    'password' => '',
    'dbname'   => 'test'
));

If you are using ZF2, then the relevant component is Zend\\Db , but creating adapters there is typically done using factories defined in module config . 如果使用ZF2,则相关组件是Zend\\Db ,但是创建适配器通常是使用在模块config中定义的工厂完成的。

And, as noted elsewhere, figuring out autoloading makes life a lot easier. 而且,正如其他地方所指出的,弄清楚自动加载功能使工作变得更加轻松。

在文件开头添加此

   require_once 'Zend/Db.php';

Zend Framework 1.x predates PHP namespaces by some time. Zend Framework 1.x早于PHP名称空间。 As a result ZF1 is not really friendly to things like the use keyword. 结果,ZF1对use关键字之类的东西并不真正友好。 If you need to use components of Zend Framework 1.x you will likely be better served by using the included autoloader component . 如果您需要使用Zend Framework 1.x的组件,则可以使用随附的自动加载器组件更好地为您提供服务。

If you are using the framework as an MVC application then this whole thing is moot and I recommend you take some time and learn how the default configuration works. 如果您将框架用作MVC应用程序,那么整个过程都没有什么用,我建议您花一些时间并了解默认配置的工作方式。 Because if you're using ZF1 as an MVC the Zend_Db component will already be on the autoloader. 因为如果将ZF1用作MVC,则Zend_Db组件将已经在自动加载器上。

Don't mix underscore names with slashes 不要将下划线名称与斜杠混用

use Zend_Db as Db

$db = Db::factory('Pdo_Mysql', array(
    'host'     => 'localhost',
    'username' => 'root',
    'password' => '',
    'dbname'   => 'test'
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM