【问题标题】:Using the error control operator in PHP with constructors将 PHP 中的错误控制运算符与构造函数一起使用
【发布时间】:2018-01-10 13:57:17
【问题描述】:

如何在构造函数中使用错误控制运算符 (@)?

如果传递了错误的参数,\DateTime 的构造函数将抛出异常并引发警告,但我宁愿能够 catch 异常并处理它,而不是收到警告。出于这个原因,我想使警告静音,例如使用@

如果我捕捉到异常,仍然会引发警告。我可以尝试使用 @ 运算符(尽管我不知道它是否也可以防止引发异常)。但我无法获得此用例的语法。

测试中的PHP版本是7.0.22。

❯ php -a
Interactive shell

php > new DateTime("abc");
PHP Warning:  Uncaught Exception: DateTime::__construct(): Failed to parse time string (abc) at position 0 (a): The timezone could not be found in the database in php shell code:1
Stack trace:
#0 php shell code(1): DateTime->__construct('abc')
#1 {main}
  thrown in php shell code on line 1

Warning: Uncaught Exception: DateTime::__construct(): Failed to parse time string (abc) at position 0 (a): The timezone could not be found in the database in php shell code:1
Stack trace:
#0 php shell code(1): DateTime->__construct('abc')
#1 {main}
  thrown in php shell code on line 1

php > $a = @new DateTime("abc");
PHP Warning:  Uncaught Exception: DateTime::__construct(): Failed to parse time string (abc) at position 0 (a): The timezone could not be found in the database in php shell code:1
Stack trace:
#0 php shell code(1): DateTime->__construct('abc')
#1 {main}
  thrown in php shell code on line 1

Warning: Uncaught Exception: DateTime::__construct(): Failed to parse time string (abc) at position 0 (a): The timezone could not be found in the database in php shell code:1
Stack trace:
#0 php shell code(1): DateTime->__construct('abc')
#1 {main}
  thrown in php shell code on line 1

php > $a = @(new DateTime("abc"));
PHP Warning:  Uncaught Exception: DateTime::__construct(): Failed to parse time string (abc) at position 0 (a): The timezone could not be found in the database in php shell code:1
Stack trace:
#0 php shell code(1): DateTime->__construct('abc')
#1 {main}
  thrown in php shell code on line 1

Warning: Uncaught Exception: DateTime::__construct(): Failed to parse time string (abc) at position 0 (a): The timezone could not be found in the database in php shell code:1
Stack trace:
#0 php shell code(1): DateTime->__construct('abc')
#1 {main}
  thrown in php shell code on line 1
php > 

php > $a = new @DateTime("abc");
PHP Parse error:  syntax error, unexpected '@' in php shell code on line 1

Parse error: syntax error, unexpected '@' in php shell code on line 1

【问题讨论】:

    标签: php error-handling constructor syntax-error php-7


    【解决方案1】:

    为什么不使用try catch

    try {
        $dt = new DateTime("abc");
    } catch(Exception $e) {
        // do wathever you want
    }
    

    【讨论】:

    【解决方案2】:

    您可以做的是使用带有set_error_handler 的自定义处理函数,这样您就可以按照您想要的方式“捕获”警告/错误。

    这是一个我用来测试正则表达式是否有效的例子,我只是使用一个空函数,因为我不需要对警告做任何事情:

    function filterRegularExpression($str) {
        set_error_handler(function() {}, E_WARNING);
        $isRegularExpression = preg_match($str, '') !== false;
        restore_error_handler();
        if($isRegularExpression){
            return $str;
        }
        return false;
    }
    

    【讨论】:

      【解决方案3】:

      您误解了产生警告的原因。警告是由全局错误处理程序产生的,因为您没有捕获异常并且它冒泡到顶部以终止程序(或者,好吧,在交互式 CLI 上产生警告)。线索是警告以“Uncaught Exception”为前缀。如果您捕捉到异常,则不会产生警告。 DateTime 构造函数不会产生自己的警告。

      【讨论】:

        猜你喜欢
        • 2019-05-21
        • 2012-03-01
        • 1970-01-01
        • 2013-09-28
        • 1970-01-01
        • 2015-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多