命名空间Autoload在Windows下运行,但在Linux上运行

问题描述 投票:11回答:5

我有以下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!';
        }
    }
}
?>

在Windows 7下运行WAMP,代码生成消息“Hello from Delegation!”但是在Linux下,我得到以下内容:

致命错误:spl_autoload():无法在第15行的/var/www/index.php中加载类modules \ standard \ handler

Windows在WAMP下运行PHP 5.3.0,Linux在Ubuntu 9.10下运行5.3.2 dotdeb软件包。

这是我的linux机箱上的配置问题,还是在不同操作系统上处理命名空间和自动加载的方式不同

php namespaces autoload
5个回答
4
投票

SPL自动加载器非常原始 - 它不知道命名空间,因此它尝试在Linux / Unix上加载一个带有\的名称的文件,路径分隔符是/否。


3
投票

Herman Radtke说他已提交补丁:

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

:■

我希望它能尽快实施。

现在我使用此解决方法:

<?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';
}

Apache上需要srttolower,因为它(与IIS相反)区分大小写。


1
投票

这是自动加载时出现的常见问题。修复是在自动加载功能中使用DIRECTORY_SEPARATOR常量。

因此,您的自动加载功能将如下所示

<?php

spl_autoload_register(function($className) {

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

});

如果您需要了解有关命名空间/类自动加载的更多信息,请访问here

谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.