The philosophy of try/catch isn't the one you described. The good way to use it is to catch different exceptions depending of what error happened. For example:
try {
$value = readDatabase();
writeDatabase($++value);
} catch (ReadErrorException $e) {
// do something
} catch (WriteErrorException $e) {
// do something
}
If you want to achieve what you're looking for, you can do as simple as using and, if you don't need more code. If you do, just use elseif.
For example, your code could become:
if (
$zombie_killer -> board_with_nail == 'failed'
&& $zombie_killer -> machette == 'failed'
&& $zombie_killer -> shotgun == 'failed'
) {
$this -> panic;
}
Also, the good way would be, using booleans as attributes:
if (
!$zombie_killer -> board_with_nail
&& !$zombie_killer -> machette
&& !$zombie_killer -> shotgun
) {
$this -> panic;
}
EDIT: After reading your comments on other answers
I propose you an alternative solution, based on what you said. Understanding that $zombie_killer is a class, you could create a public method named, for example, kill(). This method can access the public, private and protected attributes of the class.
I will write an example without exceptions first.
Inside the class:
public function kill()
{
if ($zombie_killer -> board_with_nail != 'failed') return true;
if ($zombie_killer -> machette != 'failed') return true;
if ($zombie_killer -> shotgun != 'failed') return true;
return false;
}
In the other file, the one we were talking about:
if (!$zombie_killer->kill()) {
$this->panic;
}
As you can see, the code is more clean, and the default behaviour of the function is suppose that the zombie won't be killed, and the exceptional cases would be if one of the methods used to kill the zombie succeeds. It's the opposite as how we would normally think: we would suppose we could kill the zombie, and the exceptional case would be if everything failed. But it's just a philosophical issue, you can reverse the code and it would still work.
Let's see the same with exceptions.
Class file:
public function kill()
{
if (
$zombie_killer -> board_with_nail == 'failed'
&& $zombie_killer -> machette == 'failed'
&& $zombie_killer -> shotgun == 'failed'
) {
throw new NotKilledException();
}
}
Other file:
try {
$zombie_killer->kill();
} catch (Exception $e) {
$this->panic;
}
So, you see? The exception version isn't of much help in this case, because you still must do the nested if, because the condition must accomplish three different types of kills failed. The exception is to not kill the zombie. Because you want the exceptional code $this->panic to be executed.
Now, you must choose what kind of implementation fits you more. The both are correct, and developers will see it right both. I would choose though, the first one, cause with a simple copy paste you could add more killing types to your zombie_killer, and it's cleaner to read.