The reason for the question is that I'm defining a bunch of getter methods for a class that will check if the property has been set and, if it has not, grab it from the database (I'm trying to be efficient with database calls), and set the property. In either case it then returns the value.
eg
Code:
function getValue()
{
if(is_null($this->value))
{
$this->value = $this->getValueFromDatabase("value");
}
return $this->value;
}
As cpradio says, the value taken from the database could well be a null, in which case the database would be accessed every time.
I get the gist that testing is_null and isset on the property itself isn't going to help me. I get what you're saying about marking the property as changed. Maybe something like this?
Code:
function getValue()
{
if(! property_exists($this, "db_value"))
{
$this->value = $this->getValueFromDatabase("value");
$this->db_value=true; //registers that we've got this value from the db
}
return $this->value;
}
Bookmarks