【问题标题】:dbManager class returning unexpected T_VARIABLE [closed]dbManager 类返回意外的 T_VARIABLE [关闭]
【发布时间】:2014-04-05 12:58:05
【问题描述】:

我正在尝试编写一个 php 论坛 OOP 样式的代码,但我遇到了一些困难这是我第一次编写 OOP 样式并且我不断收到错误

第 5 行出现意外的 T_VARIABLE

这是我的代码

<?php
class dbManager{
  var $MySQL_Conx;
  var $details = configManager::getConfig;
  var $mysqli = $this->dbConnect();
  public function dbSelect($sql, $mysqli){

  }
  private function dbConnect(){
    $this->CloseConnection()
    $this->MySQL_Conx = mysqli_connect($this->details['dbhost'], $this->details['dbuser'], $this->details['dbpass'], $this->details['dbdatabase'])
    if (mysqli_connect_errno()){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      exit();
    } else {
      return $MySQL_Conx;
    }
  }
}
?>

configManager::getConfig; 也返回一个包含所有详细信息的数组以连接到数据库。

【问题讨论】:

  • $this-&gt;MySQL_Conx = mysqli_connect...末尾加分号
  • 刚刚犯了同样的错误:/ @fred-ii
  • 你使用的是什么版本的 PHP
  • 你在$this-&gt;CloseConnection()也忘记了一个@user2710382
  • 你是否知道 PHP 5.0 是在一段时间前发布的......就像 10 年前

标签: php oop mysqli


【解决方案1】:

请在构造函数中赋值

<?php
class dbManager{
  var $MySQL_Conx;
  var $details = array();
  var $mysqli = '';
  //you have to assign the values in the constructor
  public function __construct()
  {
    //assign the details
    $this->details = configManager::getConfig;
    //assign the mysql connection
    $this->mysqli = $this->dbConnect()
  }

  public function dbSelect($sql, $mysqli){

  }
  private function dbConnect(){
    $this->CloseConnection()
    $this->MySQL_Conx = mysqli_connect($this->details['dbhost'], $this->details['dbuser'], $this->details['dbpass'], $this->details['dbdatabase']);
    if (mysqli_connect_errno()){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      exit();
    } else {
      return $MySQL_Conx;
    }
  }
}

【讨论】:

    【解决方案2】:

    您需要在constructor 上进行初始化。代码里面的 cmets 表示你犯的错误。

    来自 PHP 手册

    这个声明可能包含一个初始化,但是这个 初始化必须是一个常量值——也就是说,它必须能够 在编译时进行评估,并且不能依赖于运行时 信息以便进行评估。

    固定代码...

    <?php
    class dbManager{
        var $MySQL_Conx;
        var $details; 
        var $mysqli; 
    
        function __construct()   //<---- Wrapped them in a constructor
        {
            $this->details=configManager::getConfig;
            $this->mysqli=$this->dbConnect();
        }
    
        public function dbSelect($sql, $mysqli){
    
        }
        private function dbConnect(){
            $this->CloseConnection();   //<--- You forgot a semicolon
        $this->MySQL_Conx = mysqli_connect($this->details['dbhost'], $this->details['dbuser'], $this->details['dbpass'], $this->details['dbdatabase']);
        if (mysqli_connect_errno()){
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            exit();
        } else {
            return $this->MySQL_Conx; //<---- Added this keyword
        }
      }
    }
    ?>
    

    【讨论】:

    • $this-&gt;MySQL_Conx = mysqli_connect... 末尾缺少分号 ;-)
    • 这是丢失的其中一个,哈哈,实际上丢失了两个。我错过了第一个。
    • 旁注,我会将这些属性设为private,或者更好的是protected
    • 是的 .. 我在代码 cmets 中提到了所有内容 :) 请看看我是否遗漏了任何东西 mate @Fred-ii-
    • 另一个在$this-&gt;MySQL_Conx = mysqli_connect($this-&gt;details['dbhost'], .........
    猜你喜欢
    • 2012-02-26
    • 2013-12-25
    • 2012-08-25
    • 1970-01-01
    • 2023-03-14
    • 2014-02-17
    • 1970-01-01
    • 2011-09-23
    • 2013-12-15
    相关资源
    最近更新 更多