2010-05-19 17 views
11

üzerine ben şu php kodu: Standart \ handler.php \Ad alanı Autoload windows altında çalışır, ancak Linux

index.php

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

use modules\standard as std; 

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

modülleri

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

Windows 7 altında, WAMP çalıştıran kod, "Merhaba'dan Delegasyon!" Ancak Linux altında, aşağıdaki alıyorum:

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

, Windows WAMP altında PHP 5.3.0 çalıştıran ve Linux Ubuntu 9.10 altında 5.3.2 dotdeb paketi çalışıyor. böylece, bu ad hiçbir bilgiye sahip -

benim linux kutusunda bu bir yapılandırma sorunu var mı, yoksa sadece bir yolu ad alanlarında fark autoloading farklı işletim sistemleri

+0

Bu durum böyle değil, spl_autoload-register() sınıf adını küçük harfe çevirir, bu yüzden de camelCase adlarını kullanırsanız Unix'i bozar (https://bugs.php.net/bug.php?id = 53065) –

cevap

4

SPL autoloader ele alınır derece ilkel Linux/Unix'te yol ayırıcı/iken, bu isimde bir dosya yüklemeye çalışır.

+0

Başlıklar için teşekkürler. Aptal PHP. __autoload() kullanacağım ya da kendimden bir şeyler pişireceğim. – EvilChookie

2

Herman Radtke o bir yama sunmuştur diyor ki:

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

: s

Ben yakında uygulanacak umuyorum. o (IIS aykırı) vaka sentive çünkü

<?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 Apache ihtiyaç vardır: For

şimdi bu geçici çözümü kullanın.

0

Bu, otomatik yüklemede sık karşılaşılan bir sorundur. Düzeltme, autoload işlevinde DIRECTORY_SEPARATOR sabitini kullanmaktır.

Yani özdevinimli_yükle işlevi here

Teşekkür ad/sınıf autoloading ziyaret daha fazla bilgi gerekiyorsa

<?php 

spl_autoload_register(function($className) { 

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

}); 

aşağıdaki gibi görünecektir.