Mysql-php使用静态或非静态连接?

Bur*_*soy 3 php oop mysqli

我是这个主题的新手.我只是想连接我的数据库并获取数据.当使用静态连接时,它正在工作但不使用非静态.正如我所说,我对Php知之甚少,可能会遗漏一些简单的东西.尝试获取非静态时出错由于发生内部服务器错误,无法显示页面.我的代码

> <?php


class DB_Connect extends mysqli{

  //  protected static $connection;//working
       protected  $connection;   / not working


    function __construct() {

    }


    function __destruct() {


    }
    public function connect() {  
        if(!isset($this->$connection)) {

            $config = parse_ini_file('./configOop.ini'); 
            $this->$connection = new mysqli($config['dbhost'],$config['username'],$config['password'],$config['dbname']);

        }
        else{}
         return $this->$connection;
         /*
        // using this part for static connection object, working
        if(!isset(self::$connection)) {

            $config = parse_ini_file('./configOop.ini'); 
            self::$connection = new mysqli($config['dbhost'],$config['username'],$config['password'],$config['dbname']);

        }
        else{}


        return self::$connection;
        */
    }


    // Closing database connection
    public function close() {
      //  mysql_close();
    }

} 
?>
Run Code Online (Sandbox Code Playgroud)

//

    <?php  include 'db_connectOop.php'; ?>
<?php
 // error_reporting(0);
  $db=new DB_Connect();
   $dbConn=$db->connect();



   if($result =$dbConn->query("Select * from cities")or die($dbConn->error)){

   if($count=$result->num_rows){

      while($row = $result->fetch_object())
      {
       echo $row->idcities;
      }
   }
 }

?>
Run Code Online (Sandbox Code Playgroud)

Gol*_*rol 5

一个小小的错误:$this->$connection应该是$this->connection.

PHP不需要第二个$,因为它已经知道(因为->)你指的是一个属性.如果你$像你一样添加,你基本上得到局部变量的值$connection,并使用该值作为属性名称.