67

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?

CJ Dennis
  • 4,226
  • 2
  • 40
  • 69
igorsantos07
  • 4,456
  • 5
  • 43
  • 62

3 Answers3

103

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.

CJ Dennis
  • 4,226
  • 2
  • 40
  • 69
T30
  • 11,422
  • 7
  • 53
  • 57
  • 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 '21 at 03:38
15

You could try trigger_error().

alex
  • 479,566
  • 201
  • 878
  • 984
7

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

moo
  • 7,619
  • 9
  • 42
  • 40
  • 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. – too much php Jul 08 '09 at 03: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. – igorsantos07 Jul 10 '09 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 03 '16 at 09: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? – Zhigalin - Reinstate CMs Feb 20 '20 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 '20 at 21:19