Namespace Autoload Linux windows altında çalışır, ancak

2 Cevap php

Ben şu php kodu var:

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 altında, WAMP çalışan, kod "Delegasyonu Hello!" Mesajı üretir Ancak Linux altında, ben şu olsun:

Ölümcül hata: spl_autoload (): Standart \ işleyicisi \ Class modülleri hattında 15 / var / www / index.php yüklenemedi

, Windows WAMP altında PHP 5.3.0 çalışan ve Linux Ubuntu 9.10 altında 5.3.2 dotdeb paketi çalışıyor.

Bu benim linux kutusunda veya yol ad alanları ve autoloading sadece bir farka bir yapılandırma sorunu, farklı işletim sistemleri üzerinde işlenir

2 Cevap

SPL autoloader son derece ilkel - bu ad alanlarının hiçbir bilgiye sahip, bu nedenle Linux / Unix üzerinde yol ayırıcı / değil iken adına \ olan bir dosyayı yüklemeye çalışır.

Herman Radtke o bir yama sundu diyor ki:

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

: S

Ben yakında uygulanacak umuyorum.

Şimdi ben bu çözümü kullanabilirsiniz:

<?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 );
?>