...

View Full Version : Simulating Exceptions in PHP?



nerhael
04-08-2004, 05:58 PM
Let's say I have an object that represents some table. You create an empty version of the object, and populate the values individually, then call:

$objectInstance->add();

Which will validate all the fields you set, and if all is well, insert object to table.

What I want is a good way for calling functions to be told why add() might fail.

If all is good, I want it to return true ideally, since that's standard behaviour for a function, otherwise, return an array of errors. But that would also evaluate to true.

I'm wondering if there is some way of emulating an exception with this? Or is there a good way to return true on success, false otherwise and have an error list?

Pete.

dswimboy
04-08-2004, 06:44 PM
i'm stabbing in the dark here, but you could call your functions like this


if($objectInstance->add()) {
echo $error_array
}


your functions may have to be re-written to include a return statement


function add() {
$error_array = array();
$error_index = 0;
if(fopen("test.txt", "r")) {
// do whatever you want
} else {
array_push($error_array, "Could not open test.txt");
$error_index++;
}
return $error_index;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum