簡體   English   中英

可以為庫代碼而不是我的代碼關閉E_STRICT嗎?

[英]Possible to turn off E_STRICT for library code but not my code?

是否可以更改我的PHP應用程序包含includerequire_once文件的錯誤報告級別(關閉E_STRICT)?

我希望能夠看到我的代碼中出現的嚴格通知,但我正在使用PEAR MDB2,當我打開E_STRICT時,我從該代碼中獲得了警告頁面。

我知道可以使用.htaccess文件在每個目錄的基礎上更改error_reporting ,但我認為這不適用於包含的文件。 我嘗試將它放在梨文件夾中,但它沒有做任何事情。

您可以使用ini_set()在運行時動態更改error_reporting設置。 這是一個例子:

// your running code using the default error reporting setting

// set the error reporting level for your library calls
ini_set('error_reporting', E_NOTICE);

// make some library calls

// reset the error reporting level back to strict
ini_set('error_reporting', E_ALL & E_STRICT);

// more of your code

您可以定義自定義錯誤處理程序 ,並使用$errfile參數來確定錯誤的來源。 如果路徑與包含的庫的路徑匹配,則禁止顯示錯誤。 否則,將其傳遞給PHP的錯誤報告。

據我所知,這應該捕獲由庫引起的任何和所有警告和通知。

因為不需要回溯,所以對於大量觸發的消息來說,它甚至可能足夠快。

這是未經測試但應該有效,基於手冊中的示例:

<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{

    $library_path = "/path/to/library";
    if (substr($errfile,0,strlen($library_path))==$library_path)
    /* Don't execute PHP internal error handler */
     return true;
    else
    /* execute PHP internal error handler */
     return false;
}

不,不可能。

ini_set('error_reporting', E_NOTICE);

但這會影響所有函數/方法調用,即使它們已在其他/庫文件中定義。

作為一個非常臟的黑客,您可以擴展所有類並依賴於magic __call方法。 這是我的頭腦,所以不要拍我的錯別字/腦筋:

class MyDb {
    protected $pearDb; // Instantiate this in your constructor.
    public function __call() {
        $oldReporting = error_reporting(~E_STRICT);
        $result = call_user_func_array(array($this->pearDb, __FUNCTION__), func_get_args());
        error_reporting($oldReporting);
        return $result;
    }
}

如果您希望我更詳細地解決這個問題,請與我們聯系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM