简体   繁体   中英

PHP throwing error over static property that doesn't exist

I'm changing around some code in my project and PHP is throwing a very weird error about class property that no longer exists. The following is the error message:

Message: Undefined property: stdClass::$limit
File: C:\\xampp\\htdocs\\site\\Classes\\model.php
Line: 48

And here's most of the method that's throwing the error, with line 48 marked:

$this->st->execute();
$this->st->setFetchMode(PDO::FETCH_OBJ);

if($row = $this->st->fetch()) {
    return $max - $row->limit; // line 48
}

return $max;

Config::$limit used to exist, but I got rid of it when I made changes to the code. I've verified through a number of text editors that the code above is the real code saved in the PHP file, so it's not my text editor on the fritz.

What could be causing this?

$row seems to be an object of StdClass returned by PDOStatement::fetch() . Property names of this object correspond to the column names returned in your result set. If there's no limit property, that's because there's no limit column returned by your SQL query.

PDO::FETCH_OBJ means that each result row will be fetched as stdClass instance.

So the only possible reason is that $row->limit does not exist!
Add !empty($row->limit) check, try var_dump() it to make sure this is true.

The error Undefined property: stdClass::$limit doesn't mean it's looking for a static property; that's just how PHP refers to properties in general (class::property).

PDO's fetch() will return either false if there is an error or some representation of the query result. When you use

$this->st->setFetchMode(PDO::FETCH_OBJ);

that tells PDO to return the resulting rows as objects of the default class stdClass with the column names of the row as properties. So you're getting that error because the resulting row has no limit column.

You can var_dump($row) to investigate what is in fact being returned but most likely the query being executed is different from what you expected or it's doing a SELECT * and the database has changed the table definition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM