【问题标题】:Getting Exception class not found on PDO method [closed]在 PDO 方法上找不到异常类 [关闭]
【发布时间】:2014-01-07 17:56:33
【问题描述】:

我遇到了一个意外的异常。当我像这样更新数据时:

try {
  $restaurantUpd = new Restaurant();
  $restaurantUpd->updateRestaurant(array( 'restaurant_name' => Input::get('restaurant_name'),
                                          'restaurant_location' => Input::get('restaurant_location'),
                                          'restaurant_contact_email' => Input::get('restaurant_contact_email')
                                        ), $_GET['edit']);
  //ok
} catch(Exception $e) {
  die($e->getMessage());
}

它返回此错误: Warning: require_once(classes/Exeption.php): failed to open stream: No such file or directory in C:\xampp\htdocs\admintest\core\init.php on line 32

但奇怪的是我没有Exeption.php 类?另外,第 32 行指的是我的自动加载:

/*
* Autoload function for classes
*/
spl_autoload_register(function($class) {
    require_once 'classes/' . $class . '.php';
});

我在餐厅类的更新方法是这样的……

public function updateRestaurant($fields = array(), $id = null) {
    if (!$this->_db->update('rt_restaurant', $id, $fields, false)) {
        throw new Exeption('There was a problem updating');
    }
}

DB 类的更新方法是 PDO 准备、执行、获取

有什么指导吗?

【问题讨论】:

  • Exeption 表示可能有错别字
  • spl_autoload_register 如果找不到该类,则尝试请求文件,在您的程序中,它会尝试搜索在您的 CATCH 中调用的异常类,可能您使用的是旧版本的 PHP。请告诉您的 php 版本并检查它是否有异常类...
  • 我的 php 版本号是最新的... PHP 版本 5.4.22
  • 全局查找Exeption.php;你几乎可以肯定在某个地方有错字。 Exception.php 是你想要的。 (注意c。)
  • 哇......刚刚编辑了我的问题以提供更多信息,现在我看到了我的错字......对不起,当它在堆栈上时溢出它似乎更清楚(它在类方法中)

标签: php sql class autoload


【解决方案1】:

你有拼写错误 Exeption 在这一行中将是 Exception

throw new Exeption('There was a problem updating');

如果不行:

试试这个:

} catch(\Exception $e) {

throw new \Exception('There was a problem updating');

来自手册 Prefixing a name with \ will specify that the name is required from the global space even in the context of the namespace.

http://www.php.net/manual/en/language.namespaces.fallback.php

【讨论】:

  • 文件顶部是什么?任何命名空间或使用语句?
  • @rZaaaa 编辑了我的答案。
【解决方案2】:

这是类方法中的错字

public function updateRestaurant($fields = array(), $id = null) {
    if (!$this->_db->update('rt_restaurant', $id, $fields, false)) {
        throw new Exeption('There was a problem updating');
    }
}

另外...抛出异常是因为我选择 WHERE id = $ID 的 de DB 方法出错

但在 MySQL 中它不叫 id 而是 rest_id。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多