Reputation: 354
I started having a weird behavior with PHP's empty() function. Everything used to work properly, but now it doesn't, and I don't get why.
I have a tbInventory class that represents out products. One record per unit.
That class has a getBootloaderRevision method/property:
public function getBootloaderRevision() {
if (empty($this->cBootloaderRevision)) {
return null;
} else {
return $this->cBootloaderRevision;
}
}
It used to work, but now, the empty() function returns true even if the member has a value. Here is an example:
$prod = new tbInventory($db, 1009);
$test = $prod->cBootloaderRevision;
echo $prod->cBootloaderRevision . "<br>";
echo $test . "<br>";
echo (empty($prod->cBootloaderRevision) ? "1" : "02") . "<br>";
echo (empty($test) ? "1" : "0") . "<br>";
I'm loading an item from my database, an Item I know to have a BootLoader Revision. I declare and load it on the first line, then I put the value of the MEMBER itself straight in a variable. I then echo both the member and the variable to prove they both have the value.
I then echo a 1 or a 0 depending on the result of the empty() function for both the member and the variable. Here is the results:
01.07
01.07
1
0
I'm at a lost here. I do NOT understand why this is happening. I know it used to work, I haven't touched this in over a year. We made an update on the server recently, and it seems to have started acting this way since the server. I was not in charge of the update, but I think they updated both the OS and PHP.
Have you ever seen something like that???
Thank you,
Upvotes: 0
Views: 225
Reputation: 522016
The only thing that would explain this behaviour is if cBootloaderRevision
isn't a "physical" property but is being retrieved with the __get
magic method, and the class either has no __isset
method implemented or that method is returning results inconsistent with __get
. The reason is that empty
will invoke __isset
first, but $prod->cBootloaderRevision
will just invoke __get
.
See http://php.net/manual/en/language.oop5.overloading.php#object.get.
Upvotes: 2