One Line Error Suppression in Class->Method?

Discussion in 'PHP' started by ColorWP.com, Aug 25, 2011.

  1. #1
    Hello.

    I have searched a lot through the internet, but couldn't find a simple way to suppress class-generated errors like:
    $myclass->insert();
    PHP:
    You could do that easily if you are calling a function via @ like so:
    @insert();
    PHP:
    The only way I could find is to create a separate method in the myclass Class to suppress errors and show them respectively like:
    $myclass->hideErrors();
    $myclass->insert();
    $myclass->showErrors();
    PHP:
    Does anyone know a simple solution to hide error messages in a particular line only (without invoking ini_set() or error_reporting() functions)?
     
    ColorWP.com, Aug 25, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    What sort of error message is it generating? The answer really depends on what type of error and the reason it is being generated. You should be able to code without generating errors or notices.

    You could do something like this:

    
    
    function insert($errors=false) {
    
        if ($errors) {
            
        } else {
            //Suppress errors in code
        }
    }
    
    
    PHP:
     
    jestep, Aug 25, 2011 IP
  3. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,120
    Likes Received:
    100
    Best Answers:
    1
    Trophy Points:
    270
    #3
    Do you mean with Try/Catch?
     
    ColorWP.com, Aug 28, 2011 IP
  4. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #4
    
    try
    {
    $myclass->insert();
    } catch(Exception e)
    {
    //Do something or nothing.
    }
    
    PHP:
     
    ssmm987, Aug 28, 2011 IP
  5. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,120
    Likes Received:
    100
    Best Answers:
    1
    Trophy Points:
    270
    #5
    The code in the "//Do something or nothing" section is only executed if no error is returned from the Try, is this correct?
     
    ColorWP.com, Aug 29, 2011 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    Try/catch will only catch an exception which may not work in this case. Is the script throwing errors or warnings and notices?
     
    jestep, Aug 29, 2011 IP
  7. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,120
    Likes Received:
    100
    Best Answers:
    1
    Trophy Points:
    270
    #7
    This was solved already. I was using the Wordpress class wpdb to insert data which may already exists, but I didn't want to return errors if the database doesn't exist or there are duplicate keys, because this behavior is being checked at later stage and action is being taken accordingly (setting a cookie in this case). Instead I used $wpdb->hide_errors() and $wpdb->show_errors() right above and below the database insertion to avoid showing any notices to the browser, which would break the setting of cookies (headers already sent).

    $wpdb->hide_errors();
    $wpdb->insert($query);
    $wpdb->show_errors();
    Code (markup):
     
    ColorWP.com, Aug 30, 2011 IP