2017-02-21 22 views
1

우리의 자동 로더 신비한 문제가 있습니다 (리눅스에서 작동) :require_once를이

function psr4_default_autoload($class) 
{ 
    // project-specific namespace prefix 
    $prefix = 'basefolder\\'; 

    // base directory for the namespace prefix 
    $base_dir = SOURCE_DIR . '/'; 

    // does the class use the namespace prefix? 
    $len = strlen($prefix); 
    if (strncmp($prefix, $class, $len) !== 0) { 
     // no, move to the next registered autoloader 
     return; 
    } 

    // get the relative class name 
    $relative_class = substr($class, $len); 

    // replace the namespace prefix with the base directory, replace namespace 
    // separators with directory separators in the relative class name, append 
    // with .php 
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; 

    if (file_exists($file)) { 
     require_once $file; 
    } 
} 

SOURCE_DIR 절대 경로입니다. 그렇지 않으면 원래 psr4 오토로더 예제입니다. https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md

이 오토로더는 Linux PC와 서버에서 작동합니다. 그러나 Windows file_exists($file)에서 true를 반환하지만 require_once $file;은 작동하지 않습니다. $file을 되풀이하면 정확하게로드 할 파일을 반환하고 파일도 정확하게로드합니다.

오류 메시지는 다음과 같습니다

Fatal error: Class 'basedir\DatabaseAbstraction\AEntity' not found in C:\xampp\htdocs\xyz\classes\DatabaseAbstraction\Entity\UserLogin.php on line 14

그 라인은 다음과 같습니다 class UserLogin extends AEntity. 상황 :

namespace basedir\DatabaseAbstraction\Entity; 


use basedir\DatabaseAbstraction\AEntity; 

class UserLogin extends AEntity 

아이디어가 있습니까?

+0

어쨌든 나는 당신이 문제를 직면했기 때문에 내 대답을 계속하겠다. 내가 생각하기에 ... 편집에 관해서는, 오류가 발생했을 때'$ file'의 내용은 무엇인가? – NaeiKinDus

+0

'$ file'로 모든 것이 잘됩니다. ' Tekay37

+0

적어도 지금 뭔가 작동하고 있습니다. 비록 이것에 대한 수표를 추가하는 것이 좋을 수도 있습니다;) – NaeiKinDus

답변

2

Windows 디렉토리 구분 기호는 "\"이고 Linux는 "/"입니다. "DIRECTORY_SEPARATOR"키워드를 사용하여 경로를 만드는 데 사용되는 문자를 나타내야합니다. 현재 OS에 따라 런타임에 결정됩니다. 또한 그에 따라 SOURCE_DIR 재 작업해야합니다

$base_dir = SOURCE_DIR . DIRECTORY_SEPARATOR; 
[...] 
$file = $base_dir . str_replace('\\', DIRECTORY_SEPARATOR, $relative_class) . '.php'; // assuming your $base_dir follows the same logic 

: 줄 것입니다 귀하의 경우

. 그냥 레코드에 대한

+0

고마워하지만 그게 그것을 해결하지 못했습니다. 나는 나의 질문을 편집했다. 오토로더는 클래스에서 작동했지만 확장 된 클래스에서는 작동하지 않습니다. – Tekay37

+0

"/"Windows에서 PHP로 잘 작동합니다. – Jaime

+0

@Jaime : 어떤 Windows 버전입니까? 어떤 PHP 버전입니까? 나는 그것에 관심이있을 것이다. – NaeiKinDus

0

:

AEntity.php이 <? 대신 <?php 시작 파일

. 그 이유는 Windows 컴퓨터에 파일이로드되지 않았기 때문입니다.

지금 울고 있습니다.

+0

짧은 태그는 php.ini의 설정 옵션이다. 아마도 두 컴퓨터간에 다른 설정 일 것입니다. 설정에 관계없이 언제나' Jaime