php: istisna işleyicileri içinde durumları ele?

2 Cevap php

Let's say were using custom extensions of the Exception class to handle custom exceptions, like this for example:

$testObject = new testClass();

ve böyle bir özdevinimli_yükle:

function __autoload($class_name) {
    $file = $class_name.'.php';
    if (file_exists($file)) {
        include $file;  
    }else{
        throw new loadException("File $file is missing");
    }
    if(!class_exists($class_name,false)){
        throw new loadException("Class $class_name missing in $file");
    }
    return true;
}

try {
    $testObject = new testClass();
}catch(loadException $e){
    exit('<pre>'.$e.'</pre>');
}

the file testClass.php does not exist, so a loadException is called with the message: File testClass.php is missing. (and all the other details...line number etc)

i tüm hataları gizlemek ve bunun yerine bir 404 sayfa (veya 500 sayfa ...) göstermek karar verdi, bu yüzden doğal olarak ben bir loadErrorPage işlev eklemek için düşünülen kadar tüm iyiydi.

class loadException {

...

    function loadErrorPage($code){
        $page = new pageClass();
        echo $page->showPage($code);
    }
}

...

try {
    $testObject = new testClass();
}catch(loadException $e){
    $e->loadErrorPage(500);
}

ancak bu testClass.php VE pageClass.php dosyaları eksik ise, o zaman önemli bir hata yerine tercih edilen 404 sayfanın gösterildiğini açık sorunu var.

I'm confused :S How do I elegantly handle this exception within a exception handle?

2 Cevap

class pageClass yok ve stilini autloader $page = new pageClass(); tarafından yüklenen edilemezse loadErrorPage() başka bir özel durum neden olur. Bu durum yakalamak ve o sınıfın olmadan bir şey yapmak gerekiyor.

function loadErrorPage($code){
  try {
    $page = new pageClass();
    echo $page->showPage($code);
  }
  catch(Exception $e) {
    // header(...500);
    echo 'fatal error: ', $code;
  }
}

Eh, her zaman sadece pageClass.php silinemedi ...

Ben size __ özdevinimli_yükle işlevi içinde yere gitmez olduğundan emin dosyayı yapıyoruz aynı şekilde, o kaybolur olmadığından emin olmak için çok zor olmamalı, bu sadece bir dosya olduğunu varsayıyorum.