Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2003
    Location
    Toronto, Ontario, Canada
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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."


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •