Laravel 如何处理 PHP 警告?

How Laravel handles PHP warnings?(Laravel 如何处理 PHP 警告?)
本文介绍了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 实例.这是 bootstrap() 方法L42" rel="nofollow noreferrer">Illuminate/Foundation/Bootstrap/HandleExceptions.php 类.

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 警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)