PHP bir çözümleme hatası olduğunda nasıl kod özel bir fonksiyonu / biraz çalıştırabilirim

3 Cevap php

PHP'nin ayrıştırma hataları işleme özel bir hata biraz çalıştırmak için arıyorum - sözdizimi yanlış türü ve ölüm o beyaz ekranlar olsun.

(Net olmak gerekirse, böylece biçimlendirilmiş kod çalıştırırken neden olacaktır türü:

<?php
if () {
?>

)

Ben bir özel hata işleyicisi ayar bir göz vardı, ama bunun için bir şey alamadım.

3 Cevap

Başka bir fikir: Bir kendi kök sunucusu var ya da sadece yerel PC komut dosyası çalıştırmak istiyorsanız aşağıdakileri yapabilirsiniz:

Put the code to test in a new file, say failure.php. In your script (same directory), where you want to check for errors, do it this way:

$path_to_test = 'failure.php';
$result = `php -l $path_to_test`;

Bayrak -l PHP sadece kodu ayrıştırmak yapar çünkü o zaman, $result olarak ayrıştırma hata mesajları var. Bu bir şey yürütmek asla. Hatta kendi üzerinde herhangi bir ve onlardan hat numaralarını olsun eğer hata iletileri ayrıştırmak.

It isn't a Fatal error as the term is used in PHP. It is a Parse error. This means that PHP cannot understand your code and it thus never comes to execution of the code at all. Therefore you can't catch such errors. If you're sure the code to test does not contain any harmful injections, you can eval it:

if ( !@eval( 'if () {' ) )
{

    echo "An error occured.";

}

Ama nasıl ve ne zaman eval() kullanmak için tavsiye okumak emin olun. Bu oldukça tehlikeli olabilir.