該iss ue是'自動加載器'不'知道'你的'根'目錄。
的「默認」 spl_autoload作品是追加「小寫」的命名空間,如directores,您指定並追加其作爲目錄在include-path目錄的方式。
注意:它不使用'DOCUMENT_ROOT'。
要解決您遇到的問題,您必須在PHP'include_path'中包含「根」路徑。
下面是一些將「Root」目錄添加到「include_path」的代碼。
我的'根'目錄是'testmysql'。
在 '1級\(第二級)' 的 '類' 文件:
namespace Level1\Level2;
class myClass {
public static function staticLocation()
{
echo '---------- ', '<br />';
echo 'method : ', __METHOD__, '<br />';
echo 'class : ', __CLASS__, '<br />';
echo 'namespace : ', __NAMESPACE__, '<br />';
echo 'directory : ', __DIR__, '<br />';
echo 'filepath : ', __FILE__, '<br />';
}
}
的的index.php,顯示所有的細節:我的系統上
namespace Level1;
spl_autoload_extensions('.php');
spl_autoload_register();
echo '<br />Document Root: ', $_SERVER['DOCUMENT_ROOT'], '<br />';
// Define the root path to my application top level directory (testmysql)
define('APP_ROOT', $_SERVER['DOCUMENT_ROOT'] .'/testmysql');
echo '<br />', 'My Application Root directory: ', APP_ROOT, '<br />';
echo '<br />', 'This File: ', __FILE__, '<br />';
echo '<br />', 'Current Directory : ', __DIR__, '<br />';
echo '<br />', 'Include path (before): ', ini_get('include_path'), '<br />';
ini_set('include_path', ini_get('include_path') .';'. APP_ROOT .';');
echo '<br />', 'Include path (after): ', ini_get('include_path'), '<br />';
Level2\myClass::staticLocation();
輸出示例:PHP 5.3 .18在窗口xp上。
P:/developer/xampp/htdocs
My Application Root directory: P:/developer/xampp/htdocs/testmysql
This File: P:\developer\xampp\htdocs\testmysql\level1\index.php
Current Directory : P:\developer\xampp\htdocs\testmysql\level1
Include path (before): .;P:\developer\xampp\php\PEAR
Include path (after): .;P:\developer\xampp\php\PEAR;P:/developer/xampp/htdocs/testmysql;
----------
method : Level1\Level2\myClass::staticLocation
class : Level1\Level2\myClass
namespace : Level1\Level2
directory : P:\developer\xampp\htdocs\testmysql\level1\level2
filepath : P:\developer\xampp\htdocs\testmysql\level1\level2\myClass.php
那麼,首先,'class'是一個保留關鍵字。當你做'class Class'時 - php會恨你,class Class(){}'也不是你如何在php中定義類,你不需要'()'。首先驗證示例中的代碼並使用有效的代碼更新問題。 – 2014-10-12 00:53:20
對不起,它只是一個佔位符名稱,我簡化了腳本... – 2014-10-12 09:16:27