Hata kontrol operatörü algılama

1 Cevap php

Bu doğru olup olmadığını söyle lütfen. Bazı dış kitaplıkları (ne yazık ki) onu çok kullanmak çünkü benim hata işleyicisi, ben, @ hata kontrol operatör hatalarını bastırmak için kullanılan algılamak gerekiyor. Script yürütme sadece özel hata işleyicileri kullanmadığınız zaman gibi devam etmelidir.

At-işareti kullanıldığında, PHP geçici 0'a error_reporting setleri biz herhangi bir değer sıfıra error_reporting ayarlanmış bir senaryonun başında So -. Şimdi IF / ELSE büyü bazı güzel yapabilirsiniz. Önyüz olarak gösterilen herhangi bir hatayı önlemek için, biz de 0'a display_errors ayarlamak, bu error_reporting geçersiz kılar (ama biz hala büyü için değer kullanabilirsiniz).

<?php

ini_set('display_errors',0);
error_reporting(E_ALL);

function error_handler($errno, $errstr, $errfile, $errline)
{
    if (error_reporting()===0) return;
    else die();
}

set_error_handler('error_handler');

//This issues an error, but the handler will return and execution continues.
//Remove the at-sign and the script will die()
@file();

echo 'Execution continued, hooray.';
?>

Yani .. burada hiçbir yakalar var mı? Dış kitaplık benim hata işleme .. geçersiz kılar biri hariç (bu konuda herhangi bir ipucu?)

1 Cevap

Script ne göz önüne alındığında, ve bazı kullanıcı notları @ operator manual page, bu ne yapıyorsun görünüyor Tamam.

Örneğin, taras says,

I was confused as to what the @ symbol actually does, and after a few experiments have concluded the following:

  • the error handler that is set gets called regardless of what level the error reporting is set on, or whether the statement is preceeded with @

  • it is up to the error handler to impart some meaning on the different error levels. You could make your custom error handler echo all errors, even if error reporting is set to NONE.

  • so what does the @ operator do? It temporarily sets the error reporting level to 0 for that line. If that line triggers an error, the error handler will still be called, but it will be called with an error level of 0

Ve set_error_handler el ile sayfa onaylamak gibi görünüyor:

Of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator.

Burada da, yararlı olabilecek bazı kullanıcı notlar vardır; Örneğin, this one (see the begining of the code)


Ne istiyoruz "devre dışı" Eğer geliştirme ortamı devam ederken hata iletileri elde edebilmek için @ operatörü (not sure I understood the question correctly ; this might help you anyway), etkisi için ise hala, sen çığlık uzantısını ({[yükleyebilirsiniz (1)]}, manual)

(Tabii, uzantısı yükleme / installating sonra) php.ini bu ayarı, bunu doğru şekilde yapılandırmak sunulmuştur:

scream.enabled = 1

Bu uzantı sadece @ operatörü devre dışı bırakır.


And here's an example (quoting the manual) :

<?php
// Make sure errors will be shown
ini_set('display_errors', true);
error_reporting(E_ALL);

// Disable scream - this is the default and produce an error
ini_set('scream.enabled', false);
echo "Opening http://example.com/not-existing-file\n";
@fopen('http://example.com/not-existing-file', 'r');

// Now enable scream and try again
ini_set('scream.enabled', true);
echo "Opening http://example.com/not-existing-file\n";
@fopen('http://example.com/another-not-existing-file', 'r');
?>

Ve bu irade çıktı:

Opening http://example.com/not-existing-file
Opening http://example.com/not-existing-file

Warning: fopen(http://example.com/another-not-existing-file): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in example.php on line 14


I am not sure I would use this extension on a production server (where I never want errors displayed), but it is very useful on a development machine, when using old code, on an application/library that uses @ operator extensivly...