duanchu9914 2012-08-16 10:31
浏览 55
已采纳

为什么Doctrine 2的自动加载器在抛出异常时会尝试获取Exception实体?

This is the relevant code:

throw new Exception("Message");

And this is the error message I get:

Fatal error: require() [function.require]: Failed opening required 'C:\wamp\www\drupal-7.15\sites\all\modules\dbManip\Entities\Exception.php' (include_path='.;C:\wamp\bin\php\php5.3.13\pear\') in C:\wamp\bin\php\php5.3.13\pear\Doctrine\Common\ClassLoader.php on line 163

I believe Doctrine is trying to load the Exception class definition as if it were just another entity of mine, but as far as I know, Exception is native to PHP (I'm using PHP 5.3.13). My theory is that I did something wrong concerning the autoloader, but I'm not sure what. Here is what I have concerning the autoloader:

use Doctrine\ORM\Tools\Setup;

require_once("Doctrine/ORM/Tools/Setup.php");
Setup::registerAutoloadPEAR();

$classloader = new Doctrine\Common\ClassLoader('Entities', __DIR__);
$classloader->register();

What am I doing wrong here? How can use throw an exception without doctrine's autoloader trying to load the Exception entity?

  • 写回答

1条回答 默认 最新

  • dongzhuzhou4504 2012-08-16 10:37
    关注

    This is PHP at play. Not Doctrine.

    When you declare your file in a namespace, it will use the same namespace for all classes mentioned in the file. To indicate the Exception class is from the root namespace, you must make it explicit.

    throw new \Exception("Message");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部