2010-05-19 2 views
11

에 내가 가지고있는 다음과 같은 PHP 코드 : 표준 \의 handler.php \네임 스페이스 자동로드는 Windows에서 작동하지만 리눅스

index.php를

<?php 
spl_autoload_extensions(".php"); 
spl_autoload_register(); 

use modules\standard as std; 

$handler = new std\handler(); 
$handler->delegate(); 
?> 

모듈

<?php 
namespace modules\standard { 
    class handler { 
     function delegate(){ 
      echo 'Hello from delegation!'; 
     } 
    } 
} 
?> 

Windows 7에서 WAMP를 실행하면 "Hello from Delegation!"메시지가 생성됩니다. 그러나 리눅스에서, 나는 다음과 같은 얻을 :

Fatal error: spl_autoload(): Class modules\standard\handler could not be loaded in /var/www/index.php on line 15

Windows가 WAMP에서 PHP 5.3.0을 실행하고, 리눅스는 우분투 9.10 아래 5.3.2 dotdeb 패키지를 실행 중입니다. 그래서, 그것은 네임 스페이스에 대한 지식이 없습니다 -

내 리눅스 상자에이 구성 문제인가, 아니면 그냥 길 네임 스페이스의 차이와 자동 로딩은 서로 다른 운영 체제

+0

이 경우는 아니지만 spl_autoload-register()는 클래스 이름을 소문자로 변환하므로 camelCase 이름 (https://bugs.php.net/bug.php?id)을 사용하면 Unix에서 중단됩니다. = 53065) –

답변

4

스코틀랜드 프리미어 자동 로더에 처리됩니다 매우 원시적이다 Linux/Unix에서 경로 구분 기호를 사용하는 동안 /는 이름에 \를 사용하여 파일을로드하려고 시도합니다.

+0

머리를 주셔서 감사합니다. 어리석은 PHP. 나는 __autoload()를 사용하거나 내 자신의 것을 요리 할 것이다. – EvilChookie

2

허먼 Radtke 그가 패치를 제출 말한다 :

http://www.hermanradtke.com/blog/hidden-features-with-spl_autoload-and-namespaces/

:의

내가 곧 구현 될 것이다 바라고 있어요. 그것은 (IIS 반대)의 경우 sentive 때문에

<?php 
set_include_path('./classes/' . PATH_SEPARATOR . get_include_path()); 
spl_autoload_extensions('.php , .class.php'); 
spl_autoload_register(); 
function linux_namespaces_autoload ($class_name) 
    { 
     /* use if you need to lowercase first char * 
     $class_name = implode(DIRECTORY_SEPARATOR , array_map('lcfirst' , explode('\\' , $class_name)));/* else just use the following : */ 
     $class_name = implode(DIRECTORY_SEPARATOR , explode('\\' , $class_name)); 
     static $extensions = array(); 
     if (empty($extensions)) 
      { 
       $extensions = array_map('trim' , explode(',' , spl_autoload_extensions())); 
      } 
     static $include_paths = array(); 
     if (empty($include_paths)) 
      { 
       $include_paths = explode(PATH_SEPARATOR , get_include_path()); 
      } 
     foreach ($include_paths as $path) 
      { 
       $path .= (DIRECTORY_SEPARATOR !== $path[ strlen($path) - 1 ]) ? DIRECTORY_SEPARATOR : ''; 
       foreach ($extensions as $extension) 
        { 
         $file = $path . $class_name . $extension; 
         if (file_exists($file) && is_readable($file)) 
          { 
           require $file; 
           return; 
          } 
        } 
      } 
     throw new Exception(_('class ' . $class_name . ' could not be found.')); 
    } 
spl_autoload_register('linux_namespaces_autoload' , TRUE , FALSE); 
?> 
1
function __autoload($class_name) { 
$paths[] = dirname(__FILE__) . "/../libs/misc/"; 
$paths[] = dirname(__FILE__) . "/../../libs/misc/"; 
$paths[] = dirname(__FILE__) . "/../../libs/helpers/"; 
$paths[] = dirname(__FILE__) . "/../../libs/simpleimage/"; 

foreach($paths as $path) 
    { 
     if(file_exists($path.strtolower($class_name).'.class.php')){ 
     require_once($path.strtolower($class_name).'.class.php'); 
     } 
    } 
} 
1
function __autoload($class_name) 
{ 
    $class_name = strtolower(str_replace('\\', DIRECTORY_SEPARATOR, $class_name)); 

    include $class_name . '.php'; 
} 

srttolower이 아파치에 필요한 관계 :

는 지금은이 해결 방법을 사용합니다.

0

이것은 자동 로딩 중에 발생하는 일반적인 문제입니다. 해결 방법은 자동로드 기능에서 DIRECTORY_SEPARATOR 상수를 사용하는 것입니다.

그래서 자동로드 기능을 사용하면 here

감사 네임 스페이스/클래스 자동 로딩 방문에 자세한 내용이 필요한 경우

<?php 

spl_autoload_register(function($className) { 

    $className = str_replace("\", DIRECTORY_SEPARATOR, $className); 
    include_once $_SERVER['DOCUMENT_ROOT'] . '/class/' . $className . '.php'; 

}); 

을 다음과 같이됩니다.