<small id='8jEuz'></small><noframes id='8jEuz'>

  • <tfoot id='8jEuz'></tfoot>
    • <bdo id='8jEuz'></bdo><ul id='8jEuz'></ul>
    <legend id='8jEuz'><style id='8jEuz'><dir id='8jEuz'><q id='8jEuz'></q></dir></style></legend>
    <i id='8jEuz'><tr id='8jEuz'><dt id='8jEuz'><q id='8jEuz'><span id='8jEuz'><b id='8jEuz'><form id='8jEuz'><ins id='8jEuz'></ins><ul id='8jEuz'></ul><sub id='8jEuz'></sub></form><legend id='8jEuz'></legend><bdo id='8jEuz'><pre id='8jEuz'><center id='8jEuz'></center></pre></bdo></b><th id='8jEuz'></th></span></q></dt></tr></i><div id='8jEuz'><tfoot id='8jEuz'></tfoot><dl id='8jEuz'><fieldset id='8jEuz'></fieldset></dl></div>

        PHP - 将准备好的 stmt 提取到类中:致命错误“找不

        时间:2023-10-04

              <legend id='cxO4k'><style id='cxO4k'><dir id='cxO4k'><q id='cxO4k'></q></dir></style></legend>

              <tfoot id='cxO4k'></tfoot>
              <i id='cxO4k'><tr id='cxO4k'><dt id='cxO4k'><q id='cxO4k'><span id='cxO4k'><b id='cxO4k'><form id='cxO4k'><ins id='cxO4k'></ins><ul id='cxO4k'></ul><sub id='cxO4k'></sub></form><legend id='cxO4k'></legend><bdo id='cxO4k'><pre id='cxO4k'><center id='cxO4k'></center></pre></bdo></b><th id='cxO4k'></th></span></q></dt></tr></i><div id='cxO4k'><tfoot id='cxO4k'></tfoot><dl id='cxO4k'><fieldset id='cxO4k'></fieldset></dl></div>

              <small id='cxO4k'></small><noframes id='cxO4k'>

                <bdo id='cxO4k'></bdo><ul id='cxO4k'></ul>

                    <tbody id='cxO4k'></tbody>
                • 本文介绍了PHP - 将准备好的 stmt 提取到类中:致命错误“找不到类"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想将查询的结果提取到一个类中(到一个类的实例数组中).但我收到以下错误消息:致命错误:在...中找不到类类别"这是我的数据库管理器类中涉及的两个函数的代码:

                  I want to fetch the result of a query into a class (into an array of instances of a class). But I get the following error message: Fatal error: Class 'Category' not found in ... This is the code of the two functions in my database manager class that are involved:

                  public function prepareStatement($_Statement)
                  {
                      $this->preparedStmt = $this->pdo->prepare($_Statement);
                  
                      if($this->preparedStmt === FALSE)
                          throw new PreparedStmtException ("Fehler: Statement konnte nicht prepared werden.");
                      else
                          return TRUE;
                  }
                  
                  
                  public function execute($_Params = array(), $_FetchMode = NULL, $_Class = NULL)
                  {
                      # Cancel execution if no statement prepared
                      if($this->preparedStmt === null) 
                          throw new PreparedStmtException ("Fehler: Statement wurde vor execute nicht prepared.");
                  
                      try
                      {
                          # Execute PDO call with params
                          $this->preparedStmt->execute($_Params);
                  
                          # If no data is returned throw NoDataException
                          if($this->preparedStmt->columnCount() == 0)
                              throw new NoDataException;
                  
                          // else
                          // Determine which fetch mode should be called, if NULL or something != 1 || != 0 just call
                          // fetchAll without params
                          if ($_FetchMode == 1)
                               $result = $this->preparedStmt->fetchAll(PDO::FETCH_ASSOC); 
                  
                          else if ($_FetchMode == 2)
                               $result = $this->preparedStmt->fetchAll(PDO::FETCH_CLASS, $_Class); 
                  
                          else 
                              $result = $this->preparedStmt->fetchAll();
                      }
                      catch (PDOException $e)
                      {
                          # Errormanagement --> Message im live Betrieb rausnehmen
                          echo '<div style="color: red;">'.$e->getMessage().'</div>';
                          $result = FALSE;
                      }
                  
                      // If result is null throw Instance Exception, if result emtpy throw NoDataException
                      if ($result == null)
                          throw new InstanceException;
                      else if (empty($result))
                          throw new NoDataException;
                  
                      return $result;
                  }
                  

                  这是一个类中的测试函数来调用它们:

                  This is a test function in a class to call them:

                  public function test () 
                  {
                      $stmt = "SELECT * FROM tx_exhibition_category WHERE uid = 1 OR uid = 2";
                      $this->mysql->prepareStatement($stmt);
                      return $this->mysql->execute (array(), 2, "Category");
                  }
                  

                  这就是我调用测试函数的方式:

                  This is how i call test function:

                  $comment = CommentQuery::getInstance();
                  $result = $comment->test();
                  var_dump($result); // should be an array with instances of Category
                  

                  这是它应该被提取到的类:

                  And this is the class where it should be fetched into:

                  class Category {
                  
                  private $id;
                  private $name;
                  private $projectId;
                  
                  // getter and setter...
                  }
                  

                  一些附加信息:

                  • 我使用自动加载器来包含我的类.
                  • 我使用命名空间
                  • 是的,可以在所有三个函数中创建类的实例,所以
                    将包含类并使用命名空间
                  • $_Mode == 1 工作正常

                  有什么想法吗?

                  推荐答案

                  如果您的 Category 类在命名空间中,您需要将完全限定的类名传入 fetchAll.

                  If your Category class is in a namespace, you'll need to pass in a fully qualified class name into fetchAll.

                  现在,PDO 正在尝试获取根命名空间中的 Category 类.它不存在.你需要告诉 PDO 关于命名空间:

                  Right now, PDO is trying to fetch into the class Category in the root namespace. It doesn't exist. You need to tell PDO about the namespace:

                  $stm->fetchAll(PDO::FETCH_CLASS, 'Vendor\Package\Category');
                  

                  或者使用 __NAMESPACE__ 常量,如果这样更容易(并且正确):

                  Or use a __NAMESPACE__ constant if that makes it easier (and is correct):

                  $stm->fetchAll(PDO::FETCH_CLASS, __NAMESPACE__ . '\Category');
                  

                  或者,更好的是,使用 PHP 5.5+ 的 ::class 常量来获取完全限定的类名.

                  Or, even better, use PHP 5.5+'s ::class constant to ge the fully qualified class name.

                  use AcmePackageCategory;
                  $stm->fetchAll(PDO::FETCH_CLASS, Category::class);
                  

                  这篇关于PHP - 将准备好的 stmt 提取到类中:致命错误“找不到类"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP PDO 查询为 FLOAT 字段返回不准确的值 下一篇:未捕获的 PDOException 显示用户名和密码

                  相关文章

                  最新文章

                    <bdo id='UQ58k'></bdo><ul id='UQ58k'></ul>

                    <small id='UQ58k'></small><noframes id='UQ58k'>

                    <legend id='UQ58k'><style id='UQ58k'><dir id='UQ58k'><q id='UQ58k'></q></dir></style></legend>

                    <tfoot id='UQ58k'></tfoot>

                    1. <i id='UQ58k'><tr id='UQ58k'><dt id='UQ58k'><q id='UQ58k'><span id='UQ58k'><b id='UQ58k'><form id='UQ58k'><ins id='UQ58k'></ins><ul id='UQ58k'></ul><sub id='UQ58k'></sub></form><legend id='UQ58k'></legend><bdo id='UQ58k'><pre id='UQ58k'><center id='UQ58k'></center></pre></bdo></b><th id='UQ58k'></th></span></q></dt></tr></i><div id='UQ58k'><tfoot id='UQ58k'></tfoot><dl id='UQ58k'><fieldset id='UQ58k'></fieldset></dl></div>