Hello and welcome to our community! Is this your first visit?
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
    Toronto, Ontario, Canada
    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:


    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?

    "God is the history of chance"
    John Gardner

  2. #2
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    mostly in Ann Arbor
    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()) {

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

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

    "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