I don’t understand this error, perhaps I use in wrong way methods from the extended class.
Something about code: Image class is extending class Database. In Database class I have selectQuery() that works fine when I’m using it from Database class, but when I’m trying to call it in construct of Image class I got this error back.
error
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();
>Solution :
selectQuery() isn’t a static method, it needs to be called through an object. You can use $this in the child class.
Also, the child class constructor needs to call the parent class’s constructor so that the database connection will be opened.
And then you need to assign the result of selectQuery() to the $data variable so you can access the columns.
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
}
The child class shouldn’t declare its own $con variable, it inherits that from the parent automatically.