3

如何以与内置函数警告相同的方式生成自定义 PHP 警告。

例如:

php > fopen(null);
PHP Warning:  fopen() expects at least 2 parameters, 1 given in php shell code on line 1
php > fopen(null, 'w');
PHP Warning:  fopen(): Filename cannot be empty in php shell code on line 1
php > fopen(array('a'), 'w');
PHP Warning:  fopen() expects parameter 1 to be a valid path, array given in php shell code on line 1

假设我有一个功能如下:

function my_func($a, $b, $c);

我可以使用哪些代码my_func在错误调用时会引发类似的警告?

4

2 回答 2

4

您可能正在寻找 trigger_error。这些将显示标准错误并由 php.ini 中的 error_log、display_errors 等设置处理

一个最小的例子是:

if (!DoAFunction()) {
  trigger_error("DoAFunction returned false!", E_USER_ERROR);
}
于 2013-02-12T05:25:05.503 回答
0
trigger_error("Here is your herror", E_WARNING);
于 2013-02-12T05:25:28.467 回答