Return FALSE if asset does not have attribute


(Nic Hubbard) #1

What would be the best to check to see if an asset has an attribute, without raising an error while checking?


Something like:


    if ($asset->attr('username') == NULL) {
    	// Do something
    }


That will correctly execute the "do something" code, but it also raises a php error, because it is checking for an attribute which does not exist.

What is the correct way to do this?

(Greg Sherwood) #2

The $vars array is public, so you can get at it before you call attr():


if (!isset($asset->vars['username'])) {

return NULL;

} else {

return $asset->attr('username');

}



It doesn't give the asset a chance to do something clever if the attr hasn't been loaded, but I don't know how often that method does tricky stuff anyway.


(Nic Hubbard) #3

Perfect, thank you Greg!