Каждый здесь должен знать статуэтки 'или', обычно приклеенные к команде die ():
$foo = bar() or die('Error: bar function return false.');
Чаще всего мы видим что-то вроде:
mysql_query('SELECT ...') or die('Error in during the query');
Однако я не могу понять, как именно работает это утверждение «или».
Я хотел бы создать новое исключение вместо die (), но:
try{
$foo = bar() or throw new Exception('We have a problem here');
Не работает, и тоже
$foo = bar() or function(){ throw new Exception('We have a problem here'); }
Единственный способ, который я нашел, - это ужасная мысль:
function ThrowMe($mess, $code){
throw new Exception($mess, $code);
}
try{
$foo = bar() or ThrowMe('We have a problem in here', 666);
}catch(Exception $e){
echo $e->getMessage();
}
Но есть ли способ создать новое исключение сразу после оператора 'or'?
Или такая структура обязательна (я вообще не использую функцию ThrowMe):
try{
$foo = bar();
if(!$foo){
throw new Exception('We have a problem in here');
}
}catch(Exception $e){
echo $e->getMessage();
}
Изменить: я действительно хочу избежать использования if () проверки каждой потенциально опасной операции, которую я выполняю, например:
#The echo $e->getMessage(); is just an example, in real life this have no sense!
try{
$foo = bar();
if(!$foo){
throw new Exception('Problems with bar()');
}
$aa = bb($foo);
if(!$aa){
throw new Exception('Problems with bb()');
}
//...and so on!
}catch(Exception $e){
echo $e->getMessage();
}
#But i relly prefer to use something like:
try{
$foo = bar() or throw new Exception('Problems with bar()');
$aa = bb($foo) or throw new Exception('Problems with bb()');
//...and so on!
}catch(Exception $e){
echo $e->getMessage();
}
#Actually, the only way i figured out is:
try{
$foo = bar() or throw new ThrowMe('Problems with bar()', 1);
$aa = bb($foo) or throw new ThrowMe('Problems with bb()', 2);
//...and so on!
}catch(Exception $e){
echo $e->getMessage();
}
#But i'll love to thro the exception directly instead of trick it with ThrowMe function.
!$fooв последнем блоке кода? - person Dominic Rodger   schedule 31.07.2009