will not tell me whether $obj->var has not been set (doesn’t exist) or it was set to null. Is there a way to know this? I’m asking because sometimes I’m loading db results into an object and I can’t find a way to tell if a NULL value was fetched from the db or no value was fetched at all. For a long time I have assumed there’s no way to tell but I hope I could be wrong.
The reason I’m asking is that I would like to avoid this check. I use a similar workaround by using IFNULL(col, ‘’) so that en empty string will indicate the value was NULL. I use $mysqli->fetch_obj() that gets all data into an object. If I wanted to check in PHP then I’d have to first load it into array, then loop over the keys and move them to the object. Quite unpleasant.
Why do you want to this btw? Why does it matter to you how the variable got it’s null value?
It doesn’t matter how but it does matter if. If I can check that the property got assigned a value I can make a decision to not issue another db query to fetch the same data. In other words - simple caching.
You could probably do something along these lines, you may have a better way of writing it though:
$currentClass = __CLASS__; // If you are inside the Class, otherwise just use the String name
$itemInQuestion = 'nameOfProperty';
if (property_exists($currentClass , $itemInQuestion))
{
if ($currentClass->$itemInQuestion != NULL)
{
echo 'It doesnt equal null';
}
}
else
{
echo 'Property: ' . $itemInQuestion . ' does not exist';
}
Wouldn’t it be easier to keep an array with the names of all the fields you know you did query for?
That way when do a new query you can do an array_diffe of the array of the fields you want to query for now and the array of the fields you already queried for.
Great! I’ve completely forgotten about property_exists() and it happens to fit my requirements perfectly. What I do is I load all db fields of a single row into an object and every field has its equivalent property that is defined in the class. But occasionally, I need to load an extra field, for example from an external table by using JOIN, and then I want to check whether this additional field has been fetched or not (if not, then I need to issue an additional query or calculate the field as needed). property_exists() will do that very well, thanks!
Thanks for the suggestion but I think this would be too much hassle to be worth it. It would certainly work but lots of unnecesarry processing. Fortunately, property_exists() is the simplest solution.
You could add a method to the DB entities to tell whether the property exists or not. That is what I have done in the past, working it into a interface that I know will always exist for DB entities. So long as an object is an instance of the interface, the method would be known to exist. By using an interface you can also hide all implementation details from outside code, allowing the class to interpret the definition, details of the method.
interface IDBRow {
public function hasCol($col); //: bool
}
class DBRow implements IDBRow {
public function hasCol($col) {
...
}
}