>  기사  >  백엔드 개발  >  PHP에서 spl_autoload를 사용할 때 전달되는 네임스페이스 구분 기호는 Linux에서 발생하는 문제입니다.

PHP에서 spl_autoload를 사용할 때 전달되는 네임스페이스 구분 기호는 Linux에서 발생하는 문제입니다.

WBOY
WBOY원래의
2016-09-06 08:57:08786검색

use CoreConfig;
use CoreRouter;

class Framework {

public static function init() {
    require_once (__DIR__ . '/functions.php');
    // 自动加载设置
    spl_autoload_register('self::loadClass');
    define("ROOT", getcwd());
    define('APP', ROOT . '/' . Config::get('app.app_name'));
    // 设置时区
    ini_set('date.timezone', Config::get('app.timezone'));
    $router = new Router();
    // 加载路由设置
    require_once (APP . '/routes.php');
    $router->dispatch();
}

public static function loadClass($className) {
    // linux上路径
    $className = str_replace("\\", "/", $className);
    $filename = ROOT . "/" . $className . ".php";
    if(is_file($filename))
        require_once($filename);
    else
        throw new \Exception("$filename Is Not Found");
}
</p>
<p>}</p>
<p>然后我用str_replace函数替换,总感觉这样不是最好的方案</p>

                            
                        


                                                                                                                        
                     <h2>回复内容:</h2>
                      
                                                            
<p><?php</p>
<p>use CoreConfig;<br>use CoreRouter;</p>
<p>class Framework {</p>
<pre class="brush:php;toolbar:false"><code>public static function init() {
    require_once (__DIR__ . '/functions.php');
    // 自动加载设置
    spl_autoload_register('self::loadClass');
    define("ROOT", getcwd());
    define('APP', ROOT . '/' . Config::get('app.app_name'));
    // 设置时区
    ini_set('date.timezone', Config::get('app.timezone'));
    $router = new Router();
    // 加载路由设置
    require_once (APP . '/routes.php');
    $router->dispatch();
}

public static function loadClass($className) {
    // linux上路径
    $className = str_replace("\\", "/", $className);
    $filename = ROOT . "/" . $className . ".php";
    if(is_file($filename))
        require_once($filename);
    else
        throw new \Exception("$filename Is Not Found");
}
</p>
<p>}</p>
<p>然后我用str_replace函数替换,总感觉这样不是最好的方案</p>

                            
                        
            <p class="answer fmt" data-id="1020000006823690">
                                    </p>
<p>你的方法是对的,不过通常不是替换成<code>/,而是使用常量DIRECTORY_SEPARATOR

使用通用分隔符,DIRECTORY_SEPARATOR

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.