69

I have a select() method in a database class, that has an optional boolean argument $sum. This argument is used to say if the method should use COUNT(*) or not too.

I would like to show a warning, like those normal PHP errors, if I try to access $class->sum if the attribute is not set (i.e. when I call select() with $sum == false.

Is there any way to show a warning like this, or I should just echo an error and be happy?

3 Answers 3

105

If you want to generate a warning, you should write

trigger_error($yourErrorMessage, E_USER_WARNING);

trigger_error() has the $error_type parameter for setting the error level (Notice, Warning or Fatal error). The constants are, respectively:

E_USER_NOTICE             // Notice (default)
E_USER_WARNING            // Warning
E_USER_ERROR              // Fatal Error

Note that Fatal error stops the execution of subsequent PHP code, while Notice and Warning let it continue.

From PHP 5.5, you should also consider the Finally statement.

1
  • 2
    don't forget to return ; when used in e.g. __call or __callStatic, it's just a function as any other, it is not a language construct. If you don't your function may fail in some further place where it shouldn't go in the first place
    – jave.web
    Feb 25, 2021 at 3:38
15

You could try trigger_error().

2
  • 11
    ... Using E_WARNING as the 2nd argument. Jul 8, 2009 at 3:18
  • 12
    trigger_error() can only accept warnings of the E_USER family; use E_USER_WARNING instead.
    – jevon
    Jan 12, 2012 at 0:08
7

You're going the object-oriented approach, so I suggest a look into exceptions.

5
  • 2
    +1 Exceptions will give you the full backtrace information for debugging, whereas trigger_error() will not give you a useful file name / line number. Jul 8, 2009 at 3:25
  • 1
    Yes, you are right, but an exception for this problem is just so much trouble. It's an easy recoverable non-fatal error, that would happen quickly on developing; triggering an warning is enough to inform the dev about what happened. Jul 10, 2009 at 22:14
  • Warnings exist for a reason. You shouldn't just ignore them and continue on your merry way, because something has most likely but not necessarily gone wrong. Throwing Exceptions forces the programmer to deal with the error before the end of the script. If you just want to inform the dev, an Exception is still a more robust way of doing it because your program won't continue when the state is possibly no longer correct. If you want to continue, you can Try your code, Catch the error and deal with it as appropriate.
    – CJ Dennis
    Nov 3, 2016 at 9:02
  • @CJDennis and if I just want to inform that, as example, this page is using a deprecated function which should be converted some day? Feb 20, 2020 at 13:15
  • @Zhigalin-ReinstateCMs Only libraries should raise deprecation warnings. Code using deprecated functions ("this page") should not alter deprecation warnings. Deprecated functions still work ... today, so the state will be be correct (barring bugs). They might be removed in the next release of the library. If you see a deprecated function being used in "this page", the responsible thing to do is to refactor your code to use only non-deprecated functions.
    – CJ Dennis
    Feb 20, 2020 at 21:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.