I personally dislike the use of
if($var) and
if(!$var) for variable types other than boolean. I find it's too ambiguous. If you jump into a project and are trying to hunt down a bug, running across a line like
if(!$list) probably will mean nothing to you. It could mean
if the array $list is empty or
if the filename pointing to a list, $list, is an empty string or
if the integer length, $list, of the list is 0 or
if we should provide a list, as indicated by the boolean $list... and so on.
I like Stormrider's idea of isempty(), although it obviously is not a valid replacement for empty(). I frequently have sections of code like this:
PHP Code:
$var = $something->returnSomething(); // argh@empty
if(!empty($var))
{
// ...
I also use two functions, issetor() and emptyor(), which provide a quicker way to write
isset($var) ? $var : $string. These also don't handle unset variables quite right. The variables are passed by reference, which will set the variables to null or an empty string or something if they're not set. (I can't remember how exactly, but they set the unset.) This is documented, though, and I just live with it. Now I'm thinking about adding an isemptyor().
In response to the original question, I sort of do both, but there's normally a semi-valid reason to create the class (like the Response class that houses redirect(), etc.).
Bookmarks