PHP Uncaught Error: Call to a member function query() on null

PHP Uncaught Error: Call to a member function query() on null

我不明白这个错误,也许我以错误的方式使用了扩展 class 中的方法。 关于代码:图像 class 正在扩展 class 数据库。在数据库 class 中,我有 selectQuery() 当我从数据库 class 中使用它时它工作正常,但是当我试图在图像 class 的构造中调用它时我恢复了这个错误。

错误

Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\strona\myAPI\Models\Database.php:24 Stack trace: #0 C:\xampp\htdocs\strona\myAPI\Models\ImageModel.php(26): Database->selectQuery('images', 'imageid = 3333') #1 C:\xampp\htdocs\strona\myAPI\Models\ImageModel.php(117): Image->__construct(3333, 3333) #2 {main} thrown in C:\xampp\htdocs\strona\myAPI\Models\Database.php on line 24

Database.php

<?php

class Database
{
    public $con;
    public function __construct()
    {
        try {
            $this->con = new PDO('mysql:dbname=' . DB_DATABASE_NAME . ';host:' . DB_HOST, "root", "");
            $this->con->exec("set names utf8");
        } catch (PDOException $e) {
            print_r("Something went wrong: <br>" . $e->getMessage());
        }
    }

    public function selectQuery(string $where, array $selectors)
    {
        if (isset($where) && isset($selectors)) {
            $selectors = selectorsGen($selectors);
            $query = "SELECT * FROM $where  WHERE $selectors ";

            try {
                $stm = $this->con->query($query);
            } catch (PDOException $e) {
                print "Something went wrong: <br>" . $e->getMessage();
            }
            return $stm->fetch(PDO::FETCH_ASSOC);
        }
    }
//rest of database class

ImageModel.php

<?php

use Image as GlobalImage;

require "./Database.php";

class Image extends Database
{
    public $con;
    public $imageid;
    public function __construct(int $imageid, int $userid)
    {
        $selectors = array(
            "imageid" => $imageid,
            "userid" => $userid,
        );
        $where = "images";
        $data = Database::selectQuery($where, $selectors); //look here
        $this->imagestring = $data['imagestring'];
        $this->imagecomment = $data['imagecomment'];
        $this->imagedate = $data['imagedate'];
        $this->imageprivate = $data['private'];
        $this->imageorder = $data['imageorder'];
        $this->albumid = $data['albumid'];
        }
//rest of image class
}

//this call make this error
$img= new Image(3333, 3333);
echo $img->getImageComment();

selectQuery()不是静态方法,需要通过object调用。您可以在 child class.

中使用 $this

此外,child class 构造函数需要调用 parent class 的构造函数,以便打开数据库连接。

然后您需要将 selectQuery() 的结果分配给 $data 变量,以便您可以访问列。

class Image extends Database
{
    public $imageid;
    public function __construct(int $imageid, int $userid)
    {
        parent::__construct();
        $selectors = array(
            "imageid" => $imageid,
            "userid" => $userid,
        );
        $where = "images";
        $data = $this->selectQuery($where, $selectors); //look here
        $this->imagestring = $data['imagestring'];
        $this->imagecomment = $data['imagecomment'];
        $this->imagedate = $data['imagedate'];
        $this->imageprivate = $data['private'];
        $this->imageorder = $data['imageorder'];
        $this->albumid = $data['albumid'];
    }
//rest of image class
}

child class 不应该声明自己的 $con 变量,它会自动从 parent 继承。