dtutlamjasblef7982 2013-01-19 20:34
浏览 60
已采纳

命名空间和自动加载如何在基于Linux的系统中运行,PHP?

So here is my code:

<?php

function __autoload($className) {
    $paths = explode(PATH_SEPARATOR, get_include_path());
    $file = $className . '.php';
    foreach ($paths as $path) {
        $combined = $path . DIRECTORY_SEPARATOR . $file;
        if (file_exists($combined)) {
            echo $combined;
            include($combined);
            return;
        }
    }
}

$string = 'Koray';
$string = Framework\StringMethods::_normalize($string);
echo $string;
?>

The above file is called index.php. There is a file called StringMethods.php in the folder under Framework directory..

So my question is here audoloader autoloads: .\Framework\StringMethods.php because I call:

$string = Framework\StringMethods.

so the $file variable actually is= FrameWork\StringMethods. And what I do is to add .php in the end an .\ in the beginning.

But how does Framework\StringMethods will behave in a Linus system? Isn't the directory seperator: "/" in Linux?

So it will try to include: ./Framework\StringMethods.php

If correct, how to code better?

  • 写回答

1条回答 默认 最新

  • duannong1801 2013-01-19 20:40
    关注

    Can you try code bellow? Be careful for for folder/files names, because unix is case-sensitive.

    define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
    
    function __autoload($className) {
        $path = str_replace('\\', DIRECTORY_SEPARATOR, $className);
        $file = ROOT_PATH . $path . '.php';
        if (is_file($file)) {
            require_once($file);
        }
    }
    
    $string = 'Koray';
    $string = \Framework\StringMethods::_normalize($string);
    echo $string;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器