Re: Programming structures
| From: | Teodor Cimpoesu | Date: | Sun, 25 Jun 2000 15:53:08 +0000 |
| Subject: | Re: Programming structures | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-2996@lists.php.net to get a copy of this message | ||
Hi alex!
On Sat, 24 Jun 2000, alex wrote:
> I'm trying to figure out what's the best way to structure all my code and I
> have some doubts about it.. here are some thoughts:
> When it comes to handling errors I usually do one of the following:
>
> <?
> $foo = my_function() or die("some text explaining the problem");
> ?>
> Are there other better alternatives than the ones I've showed up in the
> examples above?
> What's your prefered way to handle that kind of problems?
My recent favourite one is ussing assert(). It's half a try{} catch(){},
I mean only one way :), cause you cannot go back to the context.
To use it, activate it from php.ini and say for assert.callback = assert_hook
then define assert_hook:
function assert_hook ($file, $line)
{
include 'error.inc';
print ("<!-- assert failed in $file:$line -->");
// eventually
print ("<!--");
ob_start();
var_dump ($GLOBALS);
var_dump ($WHATEVER_ELSE); //don't forget to `global' it first
print (ob_get_contents());
ob_end_clean();
print ("-->");
}
you can do anything else here, of course (and also note assert_hook
can be different for different php files.)
Another one would be set_error_handler(), which is kindof the same,
except that to use assertions you would write:
assert ($foo = my_function());
while the second one would be triggered only on PHP errors.
hope it helps,
-- teodor