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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划