Laravel如何处理PHP警告? [英] How Laravel handles PHP warnings?

查看:94
本文介绍了Laravel如何处理PHP警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Laravel连接到LDAP服务器.重要的是要说我正在使用PHP函数 ldap_connect ldap_bind ,而不是使用程序包来处理它.

I'm trying to connect to a LDAP server using Laravel. It is important to say that I'm using the PHP functions ldap_connect and ldap_bind instead of using a package to handle it.

重点是当我提供错误的用户名和密码时, ldap_bind 函数会向我们发出PHP警告.我对这个警告表示满意,并且像文档中一样,当未发生绑定时,该函数返回 false .

The point is that when I provide wrong user and password, the ldap_bind function gives to us a PHP warning. I'm OK with this warning and, as is in the documentation, the function returns false when the bind does not occur.

但是,触发此警告时,Laravel引发异常.这不是一个例外,Laravel不应抛出异常,我也不想将其作为例外处理.我只需要建立一个 if 条件,该条件将向用户返回一条消息.

But, Laravel is throwing an exception when this warning is triggered. This is not an exception, Laravel should not throw an exception and I wouldn't like to handle this as an exception; I just have to build an if condition that will return a message to the user.

Laravel是否将所有警告都捕获为异常?

Does Laravel catch all warnings as an Exception?

推荐答案

这是Laravel的预期行为.Laravel会将所有错误转换为 ErrorException 实例.这是

This is the intended behavior for Laravel. Laravel will turn any error into an ErrorException instance. Here's the bootstrap() method inside the Illuminate/Foundation/Bootstrap/HandleExceptions.php class.

public function bootstrap(Application $app)
{
    $this->app = $app;

    error_reporting(-1);

    set_error_handler([$this, 'handleError']);

    set_exception_handler([$this, 'handleException']);

    register_shutdown_function([$this, 'handleShutdown']);

    if (! $app->environment('testing')) {
        ini_set('display_errors', 'Off');
    }
}

error_reporting(-1); 将设置PHP报告所有错误(更多信息

The error_reporting(-1); will set PHP to report all errors (read more here).

这部分代码:

set_error_handler([$this, 'handleError']);

将设置自定义错误处理程序.如果检查 handleError()方法,则很明显Laravel会将任何错误转换为 ErrorException 实例.

Will set a custom error handler. If you check the handleError() method, it's pretty clear that Laravel will turn any error into an ErrorException instance.

public function handleError($level, $message, $file = '', $line = 0, $context = [])
{
    if (error_reporting() & $level) {
        throw new ErrorException($message, 0, $level, $file, $line);
    }
}

此处了解更多有关用户定义的错误处理程序的信息

Read more about user-defined error handler here.

希望这可以使事情变得清晰起来.:)

Hope this clear things up. :)

这篇关于Laravel如何处理PHP警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆