Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Minic try catch{} in PHP4

Status
Not open for further replies.

Itshim

Programmer
Apr 6, 2004
277
US
Greetings,

Has anyone ever tried to setup a 'try catch' scenario in PHP4? My reason for asking is:

I have a piece of code (we'll call it: base code) which all other code must go through to access the db. The base code performs some validation and escapes values...because the base code can return false, I need to check for errors every time it is accessed, consquently my code is littered with:
Code:
if (!$code->base_call()) {
    return false;
}
Causing what I would deem as 'code bloat', if it wasn't necessary...So to fix this problem I have been trying to devise a way to mimic the 'try catch' statements of PHP5 in PHP4.

All I need is a way to execute a bunch of statements and check if any return false.

Thanks for any suggestions,
Itshim
 
Have thought about using a custom error routine and trigger some exception?
 
Yes, I currently have a home-made error handler.

Your suggestion has got me thinking though...

[Just so you know where I'm coming from]
The methods I am talking about in my first post are for building SQL statements, and then executing those statements against the db.

My error handler logs all errors of the E_ERROR, E_USER_ERROR, E_WARNING, and E_USER_WARNING levels in an error log, and uses the level of E_USER_NOTICE for errors which can be displayed to the user.

Since none of the errors in the 'base code' generate E_USER_NOTICE errors, I don't have to worry about the error messages, I just need to know it failed, so I decided to create an object var which defaults to false, and if an error occurs that var is set to true. So in a sense the methods will silently fail. Then before executing any statements against the db I can check that var for true and if so simply return false, which is the same as the db returning an error. So I only have to check for errors once at the time of executing a statement against the db.

I think this will work, thanks!

Itshim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top