2016-06-07 62 views
0

所以這裏是我的代碼。它在我使用較長的陳述時有效,但當我使用速記時不起作用。對不起,我的英語不好。速記If/Else require_once PHP

<?php 

function __autoload($class) { 

    $path = "includes/{$class}.php"; 

    // IT WORKS LIKE THIS 

    if (file_exists($path)) { 

     require_once($path); 

    } 

    else { 

     die("File Not Found :("); 

    } 

    //But Not Like This 

    require_once() file_exists($path) ? $path : die("File Not Found :("); 

} 

?> 
+0

你不工作的替代方法有效的語法,只是不會工作require_once()file_exists($路徑)。我強烈建議你只使用作曲家自動加載器,如果你使用任何第三方代碼,你會無論如何使用它。 – JimL

+0

這是一個語法錯誤,這就是爲什麼它不起作用。 – Nehal

回答

0

file_exists($path) ? require_once($path) : die('file not found');

但我認爲,如果速記說法是正確的指定變量。我會寫這樣的代碼,

try { 
    require_once($path); 
} catch (Exception $e) { 
    die('file not found'); 
}