1. <small id='SG0vh'></small><noframes id='SG0vh'>

        <bdo id='SG0vh'></bdo><ul id='SG0vh'></ul>
    2. <legend id='SG0vh'><style id='SG0vh'><dir id='SG0vh'><q id='SG0vh'></q></dir></style></legend>

      <tfoot id='SG0vh'></tfoot>

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

        致命错误:不在对象上下文中时使用 $this

        时间:2023-07-30
          <tbody id='2Hims'></tbody>

        <small id='2Hims'></small><noframes id='2Hims'>

        <legend id='2Hims'><style id='2Hims'><dir id='2Hims'><q id='2Hims'></q></dir></style></legend>

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

                <bdo id='2Hims'></bdo><ul id='2Hims'></ul>

                  本文介绍了致命错误:不在对象上下文中时使用 $this的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有这个类用于使用 php/mysqli 连接到 mysql 数据库:

                  class AuthDB {私人 $_db;公共函数 __construct() {$this->_db = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME)or die("连接数据库有问题.错误:".mysqli_error());}公共函数 __destruct() {$this->_db->close();未设置($this->_db);}}

                  现在,我有列表用户的任何页面:

                  require_once 'classes/AuthDB.class.php';session_start();$this->_db = new AuthDB();//这条线的错误$query = "SELECT Id, user_salt, password, is_active, is_verified FROM Users where email = ?";$stmt = $this->_db->prepare($query);//绑定参数$stmt->bind_param("s", $email);//执行语句如果 ($stmt->execute()) {//绑定结果列$stmt->bind_result($id, $salt, $pass, $active, $ver);//获取第一行结果$stmt->fetch();回声 $id;}

                  现在,我看到这个错误:

                  致命错误:第 6 行中不在对象上下文中时使用 $this

                  如何修复这个错误?!

                  解决方案

                  就像错误所说的那样,您不能在类定义之外使用 $this.要在类定义之外使用 $_db,首先将其设为 public 而不是 private:

                  public $_db

                  然后,使用此代码:

                  $authDb = new AuthDb();$authDb->_db->prepare($query);//其余代码相同

                  --

                  您必须了解 $this 的实际含义.在类定义中使用时,$this 用于引用该类的对象.因此,如果您在 AuthDB 中有一个函数 foo,并且您需要从 foo 内访问 $_db,您将使用 $this 告诉 PHP 您想要 $_db 来自 foo 所属的同一对象.

                  您可能想阅读这个 StackOverflow 问题:PHP:self vs $this>

                  i have this class for connect to mysql database using php/mysqli:

                  class AuthDB {
                      private $_db;
                  
                      public function __construct() {
                          $this->_db = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME)
                          or die("Problem connect to db. Error: ". mysqli_error());
                      }
                  
                      public function __destruct() {
                          $this->_db->close();
                          unset($this->_db);
                      }
                  }
                  

                  now, i have any page for list user :

                  require_once 'classes/AuthDB.class.php';
                  
                  session_start();
                  
                  $this->_db = new AuthDB(); // error For This LINE
                  $query = "SELECT Id, user_salt, password, is_active, is_verified FROM Users where email = ?";
                  $stmt = $this->_db->prepare($query);
                  
                          //bind parameters
                          $stmt->bind_param("s", $email);
                  
                          //execute statements
                          if ($stmt->execute()) {
                              //bind result columnts
                              $stmt->bind_result($id, $salt, $pass, $active, $ver);
                  
                              //fetch first row of results
                              $stmt->fetch();
                  
                              echo $id;
                  
                  
                          }
                  

                  now, i see this error:

                  Fatal error: Using $this when not in object context in LINE 6
                  

                  How to fix this error?!

                  解决方案

                  Like the error says, you can't use $this outside of the class definition. To use $_db outside the class definition, first make it public instead of private:

                  public $_db

                  Then, use this code:

                  $authDb = new AuthDb();
                  $authDb->_db->prepare($query); // rest of code is the same
                  

                  --

                  You have to understand what $this actually means. When used inside a class definition, $this is used to refer to an object of that class. So if you had a function foo inside AuthDB, and you needed to access $_db from within foo, you would use $this to tell PHP that you want the $_db from the same object that foo belongs to.

                  You might want to read this StackOverflow question: PHP: self vs $this

                  这篇关于致命错误:不在对象上下文中时使用 $this的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:mysqli_insert_id 是否有可能在高流量应用程序中返回 下一篇:如何在 XAMPP 上启用 mysqli?

                  相关文章

                  最新文章

                1. <legend id='CUEf8'><style id='CUEf8'><dir id='CUEf8'><q id='CUEf8'></q></dir></style></legend>

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

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

                    • <bdo id='CUEf8'></bdo><ul id='CUEf8'></ul>

                      <tfoot id='CUEf8'></tfoot>