Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-08-2004, 05:58 PM   PM User | #1
nerhael
New Coder

 
Join Date: May 2003
Location: Toronto, Ontario, Canada
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
nerhael is an unknown quantity at this point
Simulating Exceptions in PHP?

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.
__________________
"God is the history of chance"
John Gardner
nerhael is offline   Reply With Quote
Old 04-08-2004, 06:44 PM   PM User | #2
dswimboy
Regular Coder

 
dswimboy's Avatar
 
Join Date: Nov 2003
Location: mostly in Ann Arbor
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
dswimboy is an unknown quantity at this point
i'm stabbing in the dark here, but you could call your functions like this
PHP Code:
if($objectInstance->add()) {
     echo 
$error_array

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

PHP Code:
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;

__________________
"There is more than one way to do it."
dswimboy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:49 AM.


Advertisement
Log in to turn off these ads.