PHP 基礎(chǔ)篇 - PHP 錯(cuò)誤級(jí)別詳解

一、前言

最近經(jīng)??吹焦ぷ?2 年左右的童鞋寫(xiě)的代碼也會(huì)出現(xiàn)以靜態(tài)方法的形式調(diào)用非靜態(tài)方法,這是個(gè) Deprecated 級(jí)別的語(yǔ)法錯(cuò)誤,代碼里不應(yīng)該出現(xiàn)的。對(duì)方很郁悶,說(shuō):為什么我的環(huán)境可以正常運(yùn)行呢?

二、詳解

代碼會(huì)不會(huì)報(bào)錯(cuò),以及你能不能看到報(bào)錯(cuò)信息由 PHP 配置中以下兩個(gè)參數(shù)影響,目前線上主流的配置如下(php.ini 文件中):

error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off

下面詳細(xì)介紹這兩個(gè)參數(shù):

1. error_reporting

首先來(lái)說(shuō)說(shuō) PHP 的錯(cuò)誤級(jí)別,代碼會(huì)不會(huì)報(bào)錯(cuò)由 error_reporting 參數(shù)決定,PHP 的錯(cuò)誤級(jí)別種類(lèi)如下(點(diǎn)擊查看中文版):

E_ALL             - All errors and warnings (includes E_STRICT as of PHP 5.4.0)
E_ERROR           - fatal run-time errors
E_RECOVERABLE_ERROR  - almost fatal run-time errors
E_WARNING         - run-time warnings (non-fatal errors)
E_PARSE           - compile-time parse errors
E_NOTICE          - run-time notices (these are warnings which often result
                    from a bug in your code, but it's possible that it was
                    intentional (e.g., using an uninitialized variable and
                    relying on the fact it is automatically initialized to an
                    empty string)
E_STRICT          - run-time notices, enable to have PHP suggest changes
                    to your code which will ensure the best interoperability
                    and forward compatibility of your code
E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's
                    initial startup
E_COMPILE_ERROR   - fatal compile-time errors
E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
E_USER_ERROR      - user-generated error message
E_USER_WARNING    - user-generated warning message
E_USER_NOTICE     - user-generated notice message
E_DEPRECATED      - warn about code that will not work in future versions
                    of PHP
E_USER_DEPRECATED - user-generated deprecation warnings 

其中 WARNING、NOTICE、DEPRECATED 等錯(cuò)誤級(jí)別會(huì)拋出一個(gè)錯(cuò)誤,但不會(huì)終止程序運(yùn)行。

2. display_errors

再來(lái)看一下 display_errors 參數(shù),該參數(shù)是是否展示錯(cuò)誤信息,只有開(kāi)啟才會(huì)顯示錯(cuò)誤信息(值可以為 on|off、true|false、1|0)。

開(kāi)發(fā)環(huán)境最好設(shè)置為開(kāi)啟,盡早發(fā)現(xiàn)問(wèn)題,但生產(chǎn)環(huán)境一定要關(guān)閉。(點(diǎn)擊查看中文版官方文檔

3. 問(wèn)題演示

下面使用 PHP 的運(yùn)行時(shí)配置,改變 PHP 的 error_reporting 和 display_errors,演示上面的問(wèn)題。代碼如下:

<?php

// error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);  // 該級(jí)別下面的代碼不會(huì)拋出錯(cuò)誤
error_reporting(E_ALL & ~E_NOTICE);
ini_set("display_errors", "on");

class Foo
{
    public function test()
    {
        echo 'test';
    }
}

Foo::test();

運(yùn)行該代碼會(huì)輸出 test,但同時(shí)會(huì)拋出一個(gè) Deprecated 級(jí)別的錯(cuò)誤。這種情況應(yīng)該杜絕,規(guī)范開(kāi)發(fā),從基礎(chǔ)做起,人人有責(zé)!


本文首發(fā)于馬燕龍個(gè)人博客,歡迎分享,轉(zhuǎn)載請(qǐng)標(biāo)明出處。
馬燕龍個(gè)人博客:http://www.mayanlong.com
馬燕龍個(gè)人微博:http://weibo.com/imayanlong
馬燕龍Github主頁(yè):https://github.com/yanlongma

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容