具有大写和小写类名的自动加载器

发布于 2024-07-15 04:48:41 字数 370 浏览 4 评论 0原文

我在 php 中使用此类进行自动加载。 http://pastebin.com/m75f95c3b

但是当我在某个地方

class Bar extends Foo

并且我有一个名为 foo.class 的文件。 php 找不到该类。 但是当我将文件名更改为 Foo.class.php 时,它会找到该类。

我正在尝试向我的类添加一些功能,以便无论文件名是否以大写字母开头,都可以始终找到该文件(如果存在)。 但到目前为止我还没有成功。

任何人?

I am using this class in php for autoloading.
http://pastebin.com/m75f95c3b

But when I have somewhere

class Bar extends Foo

And I have a file called foo.class.php it won't find the class.
But when i chagne the filename to Foo.class.php it will find the class.

I am trying to add some functionallity to my class to always find the file if its there no matter wether the filename starts with a capital or not. But so far I haven't scuceeded.

Anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小嗲 2024-07-22 04:48:41

如果您的所有文件都有小写名称,请将 strtolower 应用于类的每个方法中的 $className 变量值:

$className = strtolower($className);

但我建议您起草一些编码指南,供每个开发人员使用必须坚持。 否则,您将必须测试使用大写和小写字母编写文件名的 2<文件名长度> 种可能性。

If all of your file have lowercase names, apply strtolower to the $className variable value in each method of your class:

$className = strtolower($className);

But I suggest that you draft some coding guidelines that every developer has to stick to. Otherwise you will have to test each of the 2<length of file name> possibilities of writing a file name using uppercase and lowercase letters.

终陌 2024-07-22 04:48:41

Gumbo 的解决方案是最好的,几乎每个人都在使用。 我不明白你为什么不喜欢。 当然可以先检查类名大写的文件是否存在,如果不存在再检查小写的版本是否存在。 这绝对不比 Gumbo 的解决方案更好。 毕竟“你的”程序员必须遵循一些约定/规则。

Gumbo's solution is the best one and that's what almost everyone uses. I do not understand why you do not like. Of course you can first check whether a file with the class name capitalized exists or not, and if not then check whether lowercased version exists or not. That's definitely not better than Gumbo's solution. After all "your" programmers have to follow some conventions/rules.

自由范儿 2024-07-22 04:48:41

我会或多或少地这样做,

function __autoload($class_name) {
    $upperName = strtoupper(substr($class_name,0,1)) . substr($class_name, 1);
    $lowerName = strtolower(substr($class_name,0,1)) . substr($class_name, 1);

    //Check to see if the class file exists as-is
    if(file_exists($class_name . '.php')) {
        require_once($class_name . '.php');
    }elseif(file_exists($upperName . '.php')) {
        require_once($upperName . '.php');
    }elseif(file_exists($lowerName . '.php')) {
        require_once($lowerName . '.php');
    }else {
        throw new Exception("The class $class_name could not be found.");
    }
}

当出现不同的命名约定时,添加其他条件很容易。

I'd do it more or less like this

function __autoload($class_name) {
    $upperName = strtoupper(substr($class_name,0,1)) . substr($class_name, 1);
    $lowerName = strtolower(substr($class_name,0,1)) . substr($class_name, 1);

    //Check to see if the class file exists as-is
    if(file_exists($class_name . '.php')) {
        require_once($class_name . '.php');
    }elseif(file_exists($upperName . '.php')) {
        require_once($upperName . '.php');
    }elseif(file_exists($lowerName . '.php')) {
        require_once($lowerName . '.php');
    }else {
        throw new Exception("The class $class_name could not be found.");
    }
}

It's easy to enough to add other conditionals as different naming conventions arise.

柒七 2024-07-22 04:48:41

我使用 __autoload() 函数,该函数考虑了几种不同的文件命名约定,以提供与以前的开发人员的向后兼容性。

它对每个约定执行一个简单的循环,并检查该文件是否存在。 如果文件存在,则加载它。

对于我的函数,我对不同的文件扩展名执行此操作,例如 .inc、.class 或 .inc.php 。 您可以执行相同的操作,但搜索上方和下方的第一个字符。

我会将其放在 searchForClassFile() 方法中,在 else 部分中添加注释“找到一个文件”。

编辑(更多信息):

我不是递归地进入类目录寻找正确的文件,而是将类名映射到特定位置。 这是一种常见的做法。 例如,foo_bar 映射到 [CLASS_DIRECTORY]/foo/bar.[EXTENSION]。 在我们的例子中,我们检查了几个不同的扩展。

在您的情况下,您必须做出有关如何搜索类文件的设计决策,但要修改代码:

} else {
  // Found a file
  if ($f == $className . self::$classFileSuffix ||
        $f == strtolower($className) . self::classFileSuffix) {
    return $subPath;
  }
}

您可以编写一个仅降低第一个字符的函数,而不是 strtolower() ,或者如果使用 PHP > ,则可以编写一个仅降低第一个字符的函数。 5.3.0(未正式发布)使用lcfirst()函数。

I use an __autoload() function that takes into account several different file naming conventions to provide backwards compatibility with previous developers.

It does a simple loop over each convention and checks to see if that file exists. If the file exists, then it loads it.

For my function, I do this for different file extensions, such as .inc, .class or .inc.php . You could do the same, but search for upper and lower first characters.

I would put this in the searchForClassFile() method, in the else part with the comment 'Found a file'.

EDIT (more information):

Rather than recursively descend into a class directory looking for the correct file, I map the class name to a specific location. This is a common practice. For example, foo_bar is mapped to [CLASS_DIRECTORY]/foo/bar.[EXTENSION]. In our case, we check several different extensions.

In your case, you have to make a design decision about how you want to search for the class file, but modifying your code:

} else {
  // Found a file
  if ($f == $className . self::$classFileSuffix ||
        $f == strtolower($className) . self::classFileSuffix) {
    return $subPath;
  }
}

Instead of strtolower() you could write a function that only lowers the first character, or if using PHP > 5.3.0 (not officially released) use the lcfirst() function.

洛阳烟雨空心柳 2024-07-22 04:48:41

您可以使用自动加载器扫描目录中的 .php 文件,并使用正则表达式实际查看每个文件的内容以查找诸如“class Foo”之类的模式。 当然,您需要缓存它,最好使用 class => 形式的数组。 目录。 您还需要确保正确使缓存失效,例如,当文件夹中有新的 .php 文件时。

我为我的网站这样做,效果很好。

You can use an autoloader that scans the directories for .php files, and actually looks into the contents of each file for patterns such as "class Foo" using a regular expression. You will need to cache this, of course, preferably by using an array of the form class => directory. You also need to make sure you invalidate the cache correctly, e.g. when there are new .php files in the folder.

I do this for my website, and it works great.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文