Is there any way to show, or throw, a PHP warning?

PhpWarnings

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

You could try trigger_error().

Solution 3 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionigorsantos07View Question on Stackoverflow
Solution 1 - PhpT30View Answer on Stackoverflow
Solution 2 - PhpalexView Answer on Stackoverflow
Solution 3 - PhpmooView Answer on Stackoverflow