У меня есть следующий код php:
index.php
<?php
spl_autoload_extensions(".php");
spl_autoload_register();
use modules\standard as std;
$handler = new std\handler();
$handler->delegate();
?>
modules\standard\handler.php
<?php
namespace modules\standard {
class handler {
function delegate(){
echo 'Hello from delegation!';
}
}
}
?>
В соответствии с Windows 7, выполняя WAMP, код создает сообщение "Привет от Делегации!" однако в соответствии с Linux, я получаю следующее:
Фатальная ошибка: spl_autoload (): Класс modules\standard\handler не мог быть загружен в/var/www/index.php на строке 15
Windows выполняет PHP 5.3.0 под WAMP, и Linux выполняет 5.3.2 dotdeb пакета в соответствии с Ubuntu 9.10.
Это проблема конфигурации о моем поле Linux или просто различие в способе, которым пространства имен и автозагрузка обрабатываются в различных операционных системах
Автозагрузчик SPL чрезвычайно примитивен - он не знает пространств имен, поэтому пытается загрузить файл с \ в его имени, в то время как в Linux / Unix путь разделитель есть / нет.
Герман Радке говорит, что он представил патч :
http://www.hermanradtke.com/blog/hidden-features-with-spl_autoload-and-namespaces/
:s
Я надеюсь, что он скоро будет реализован.
Пока что я использую это обходное решение :
<?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 );
?>