Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP获取类内部错误_Php - Fatal编程技术网

使用PHP获取类内部错误

使用PHP获取类内部错误,php,Php,在使用PHP访问类中的变量时,我面临以下错误 错误: 注意:未定义变量:第28行/var/www/oditek.in/subhra/database.php中的pdo 致命错误:无法访问第28行/var/www/oditek.in/subhra/database.php中的空属性 我在下面解释我的代码 <?php define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASSWORD', ''); def

在使用PHP访问类中的变量时,我面临以下错误

错误: 注意:未定义变量:第28行/var/www/oditek.in/subhra/database.php中的pdo
致命错误:无法访问第28行/var/www/oditek.in/subhra/database.php中的空属性

我在下面解释我的代码

<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'spesh');
/**
* 
*/
class DBOperations
{

    private $pdo;
    function __construct()
    {
        $this->connect();
    }
    function __destruct() {
        // $this->close();
    }
    public function connect() {
        $dsn = "mysql:host=".DB_HOST.";dbname=".DB_DATABASE.";charset=utf8mb4";
        $options = [
          PDO::ATTR_EMULATE_PREPARES   => false, // turn off emulation mode for "real" prepared statements
          PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION, //turn on errors in the form of exceptions
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, //make the default fetch be an associative array
        ];
        try {
          $this->$pdo = new PDO($dsn, DB_USER, DB_PASSWORD, $options);
        } catch (Exception $e) {
          error_log($e->getMessage());
          exit('Something weird happened'); //something a user can understand
        }
    }
    public function db_insert($table,$values, $columns=array()){
        if($table=="" || $values==""){
            return false;
        }
        $columnstr=$valuestr="";
        $valprepArr="";
        if(count($columns)>0){
            $columnstr=implode(",", $columns);
            $columnstr="(".$columnstr.")";
        }
        foreach($values as $key=>$val){
            if($valuestr !=""){
                $valuestr.=",?";
            }else{
                $valuestr.="?";
            }
            array_push($valprepArr, $val);
        }
        $sql = "INSERT INTO ".$table." ".$columnstr." VALUES (".$valuestr.")";
        $stmt = $this->$pdo->prepare($sql);
        $stmt->execute($valprepArr);
        return $this->$pdo->lastInsertId();
        $stmt = null;
    }
}
?>

在类文件中,我得到了这个错误,我想解决这个问题。

这一行:

$this->$pdo = new PDO($dsn, DB_USER, DB_PASSWORD, $options);
应该是:

 $this->pdo = new PDO($dsn, DB_USER, DB_PASSWORD, $options);
无论你在哪里使用
$this->$pdo
都应该是
$this->pdo

更多信息

错误:注意:未定义变量:pdo in /var/www/oditek.in/subhra/database.php,第28行

解决方案:第28行
$this->$pdo
应该是
$this->pdo

致命错误:无法访问中的空属性 /var/www/oditek.in/subhra/database.php,第28行


$this
之后,需要调用类中定义的属性、方法或函数,或创建
$this
对象的位置。

$this->pdo
不是
$this->$pdo