views:

170

answers:

4

Hey there SO,

I was just wondering if there was any way to make my object return false if placed in an if statement. The reason is I'm making a custom exception class. I would like to be able to do something along the lines of

class Dog{

   public function lookAround()
   {
        if(seeDog())
        {
            return bark;
        }
        else
        {
            return sit;
        }
   }

   public function seeDog()
   {
        if(object == Dog)
        {
             return "husky";
        } 
        else
        {
             return Dog_Exception(no_see);
        }
   }
}

I understand that this is a horrendously stupid example. However, the point is that as this object stands, the if(seeDog()) test will evaluate true for both string "husky" and for the Dog_Exception object. If at all possible, i'd like Dog_Exception to evaluate to false, if placed in an if condition. That way I wouldn't have to, for example, use a construct like if(typeof(seeDog()) == Dog_Exception) etc. I doubt this is possible, but any help would be great. Thanks!

+5  A: 

Why are you returning an exception? Shouldn't you be throwing it?

Greg
+1. What exactly do you want lookAround() to return? A string? A boolean? An exception?
thedz
All I want is an object, lets call it DogObject, that if I type $dog = new DogObject();if(!$dog){ echo "Yay, $dog returned false!".}echo's "yay...". Essentially, I want to know if it's possible to make an object return false.
Ethan
+4  A: 

what about something like a try catch? you need to throw the exception not return it.

function inverse($x) {
if (!$x) {
    throw new Exception('Division by zero.');
}
    else return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

via: http://us3.php.net/manual/en/language.exceptions.php

easement
+1  A: 
class Dog{

   public function lookAround()
   {
     try
     {
            if(seeDog())
            {
                return bark;
            }   
     }
     catch(Dog_Exception $e)
     {
      return sit;
     }
   }

   public function seeDog()
   {
        if(object == Dog)
        {
             return "husky";
        } 
        else
        {
             throw Dog_Exception(no_see);
        }
   }
}
detour
It should be `throw new Dog_Exception(no_see);`, unless `Dog_Exception` is a function which returns an instance of `Dog_Exception`.
Ionuț G. Stan
A: 

I was attempting to do this myself and found a solution that appears to work.

In response to the others who were trying to answer the question by telling the asker to use a different solution, I will also try to explain the reason for the question. Neither the original poster or I want to use an exception, because the point is not to use exception handling features and put that burden on any code we use this class in. The point, at least for me, was to be able to use this class seamlessly in other PHP code that may be written in a non-object-oriented or non-exception-based style. Many built-in PHP functions are written in such a way that a result of false for unsuccessful processes is desirable. At the same time, we might want to be able to handle this object in a special way in our own code.

For example, we might want to do something like:

if ( !($goodObject = ObjectFactory::getObject($objectType)) ) {
  // if $objectType was not something ObjectFactory could handle, it
  // might return a Special Case object such as FalseObject below
  // (see Patterns of Enterprise Application Architecture)
  // in order to indicate something went wrong.
  // (Because it is easy to do it this way.)
  //
  // FalseObject could have methods for displaying error information.
}

Here's a very simple implementation.

class FalseObject {
  public function __toString() {
    // return an empty string that in PHP evaluates to false
    return '';
  }
}

$false = new FalseObject();

if ( $false ) {
  print $false . ' is false.';
} else {
  print $false . ' is true.';
}

print '<br />';

if ( !$false ) {
  print $false . ' is really true.';
} else {
  print $false . ' is really false.';
}

// I am printing $false just to make sure nothing unexpected is happening.

The output is:

is false. is really false.

I've tested this and it works even if you have some declared variables inside the class, such as:

class FalseObject {
  const flag = true;
  public $message = 'a message';
  public function __toString() {
    return '';
  }
}


A slightly more interesting implementation might be:

class FalseException extends Exception {
  final public function __toString() {
    return '';
  }
}

class CustomException extends FalseException { }

$false = new CustomException('Something went wrong.');

Using the same test code as before, $false evaluates to false.

Doug Treadwell