2

I'm interested in how could I create my own function allowing it to be executed without error logging, but only if executed with @ operator, like @function();?

Function:

public function query($sql, $id = false){
    $this->query_id = mysqli_query($this->connection, $sql);
    if(!$this->query_id){
        // I want to suppress this error call here...
        psyo::error("Error while executing query (sql: {$sql}).");
        return NULL;
    }else{
        $this->result = mysqli_store_result($this->connection);
        $this->affected = mysqli_affected_rows($this->connection);
        return $id ? $this->query_id : $this;
    }
}

P.S. I'm using my own error handling class, and it isn't operated with error_log(); or so.

Thanks in advance!

4
  • Not sure what you mean. Can you elaborate? Commented Apr 5, 2011 at 16:04
  • Is this what you're trying to achieve? php.net/manual/en/language.operators.errorcontrol.php#98895 Commented Apr 5, 2011 at 16:09
  • 1
    If I call simply query();, psyo::error(); is executed; if I call @query(); it's not. Commented Apr 5, 2011 at 16:10
  • Thanks for the accept. Please be sure to also upvote @Josh Davis's answer if you haven't already. He gave a similar, and very correct, answer as mine while I was still writing/testing mine. He deserves some love as well. Commented Apr 5, 2011 at 16:43

3 Answers 3

5

I don't think there's a way to know for sure whether the @-operator was used to call a function, but you can use error_reporting() to test for the current level, which is 0 when the @-operator is being used.

function test()
{   
    var_dump(error_reporting());
}

test();
@test();
Sign up to request clarification or add additional context in comments.

Comments

2

The @ operator is just shorthand for the following:

$oldReportingLevel = error_reporting();
error_reporting(0);
 // execute @'d code here
error_reporting($oldReportingLevel);

In other words, @ temporarily sets the error reporting level to 0. While you can't check whether or not the current error reporting level is due to an @ operator or not, you can check the current error reporting level, and bypass your own error handling if the level is 0.

What you probably want is this:

if (error_reporting() != 0) {
 psyo::error("Error while executing query (sql: {$sql}).");
}

Comments

2

If I am understanding you right, you want a way to suppress your own errors from your class call. One way is to make a boolean variable in the class, showErrors and then in the psyo::error function, check that variable. If showErrors is true, display them, else do not display them but log them (or handle them however you want).

If you mean that you want the @ to suppress your class errors that are passed to the error function, then I think your error function would need to "trigger errors". See trigger_error for more details on that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.