Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:设置错误处理程序和visibily_Php_Error Handling_Visibility - Fatal编程技术网

PHP:设置错误处理程序和visibily

PHP:设置错误处理程序和visibily,php,error-handling,visibility,Php,Error Handling,Visibility,在我的类构造函数中,我有以下内容: set_error_handler(array( $this, '_custom_error_handler' )); 在同一类中,我定义了以下方法: protected function _custom_error_handler($error_number, $error_string, $error_file, $error_line) 当代码中的某些内容出现错误时,我会收到以下警告: 警告:无效回调_自定义\u错误\u处理程序,无法

在我的类构造函数中,我有以下内容:

set_error_handler(array(
    $this,
    '_custom_error_handler'
));
在同一类中,我定义了以下方法:

protected function _custom_error_handler($error_number, $error_string, $error_file, $error_line)
当代码中的某些内容出现错误时,我会收到以下警告:

警告:无效回调_自定义\u错误\u处理程序,无法访问受保护的方法


为什么这个类(或它的子类?)不能访问这个受保护的方法?受保护的方法不应该是可访问的吗?

受保护的方法只能从类或子类内部访问


在本例中,set\u error\u处理程序正在调用一个方法,set\u error\u处理程序在您的类之外。因此,它必须是公共的。

回调确实允许访问受保护的方法,但无论PHP内核在哪里启动,您的回调都不属于该类。请注意自己调用
call\u user\u func()
的区别。您应该仔细阅读PHP编码标准。它说:方法名称的前缀不应该加一个下划线来表示受保护的或私有的可见性。我觉得“set_error_handler在你的类之外”部分不是很清楚,因为它使用
$this
没有错误,很明显它在类内部。我假定您指的是最终调用回调函数的上下文。