11

次のphpコードがあります。

index.php

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

use modules\standard as std;

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

モジュール\標準\handler.php

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

WAMP を実行している Windows 7 では、コードは "Hello from Delegation!" というメッセージを生成します。ただし、Linuxでは、次のようになります。

致命的なエラー: spl_autoload(): クラス modules\standard\handler を 15 行目の /var/www/index.php にロードできませんでした

Windows は WAMP で PHP 5.3.0 を実行しており、Linux は Ubuntu 9.10 で 5.3.2 dotdeb パッケージを実行しています。

これは私の Linux ボックスの構成の問題ですか、それとも、異なるオペレーティング システムで名前空間と自動読み込みが処理される方法の違いだけですか?

4

5 に答える 5

4

SPL オートローダは非常に原始的です。名前空間を認識しないため、名前に \ が含まれるファイルをロードしようとしますが、Linux/Unix ではパス区切り文字は / ではありません。

于 2010-05-20T18:59:25.330 に答える
3

Herman Radtke はパッチを提出したと言っています:

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 );
?>
于 2010-05-23T23:32:50.020 に答える
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');
        }
    }
}
于 2011-01-31T23:16:30.150 に答える