On Mon, Oct 22, 2001 at 02:57:44PM +1000, Brian White wrote:

Preface to these ramblings: Exception handling is planned for the next
version of the Zend engine.  For details, see

http://www.zend.com/zend/future.php

> The nasty thing then comes in that if any task fails, I have to stop. So I
> create a convention that every task returns "1" for success, "0" for
> failure. Tasks can also be nested. So this gives me a structure like
> this:
> 
>      if ( ! GrabLock() )
>          return 0;
> 
>      $retval = 0;
>      switch(0){ default:
> 
>          if ( ! RunTask1() )
>              break;
> 
>          if ( ! RunTask2() )
>              break;
> 
>          if ( ! RunTask3() )
>              break;
> 
>          $retval = 1;
>      }
> 
>      ReleaseLock();
>      return $retval;
> 
> This works, but it is cumbersome, you have to be VERY careful and there are
> other complications in terms of reporting sensible error messages when
> tasks are shared.

This is probably no less messy, but it does allow you to handle an
arbitrary number of steps, possibly with arguments, without having to
add another if check as you're doing above.  This also lets you know
easilly which step failed:

<?

function first($a)  { if ($a) return true; else return false; }
function second($a) { if ($a) return true; else return false; }
function third($a)  { if ($a) return true; else return false; }

function GrabLock() { echo "Grabbing.\n"; return true; }
function ReleaseLock() { echo "Releasing.\n"; return true; }

$ret = true;
$funcs = Array('first','second','third');
$args = Array(1,1,1);   // Change one to zero to trigger a failure

if (!GrabLock())
    return 0;   

while ($ret = $funcs[0]($args[0]))
{
    array_shift($funcs);
    array_shift($args); 
    if (!count($funcs)) break;
}
 
ReleaseLock();

if (count($funcs)) echo "Failed at $funcs[0].\n";

?>

Matt

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to