這些代碼是從書< <專業的PHP MVC >>複製,我覺得他們錯過了一些線條,有PHP SPL自動加載寄存器問題
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
但沒有使preg_split
和我想不通行
$file = strtolower(str_replace("\\", DIRECTORY_SEPARATOR, trim($class, "\\"))).".php";
它trimed 雙反斜槓先試試取代雙反斜槓用DIRECTORY_SEPARATOR,怎麼?
它會有什麼情況雙反斜槓在類名?
代碼:
= ------
function autoload($class)
{
$paths = explode(PATH_SEPARATOR, get_include_path());
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
$file = strtolower(str_replace("\\", DIRECTORY_SEPARATOR, trim($class, "\\"))).".php";
foreach ($paths as $path)
{
$combined = $path.DIRECTORY_SEPARATOR.$file;
if (file_exists($combined))
{
include($combined);
return;
}
}
throw new Exception("{$class} not found");
}
class Autoloader
{
public static function autoload($class)
{
autoload($class);
}
}
spl_autoload_register('autoload');
spl_autoload_register(array('autoloader', 'autoload'));
這些是反斜槓,而不是正斜槓。在第一個例子中,你有三個反斜槓,第二個例子中有兩個反斜槓。哪個是對的?第一個反斜槓在任何情況下都是[轉義字符](http://php.net/manual/en/regexp.reference.escape.php)。 – jurgemaister 2014-12-05 11:20:33