简体   繁体   English

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

[英]autoloader with upper and lowercase classname

I am using this class in php for autoloading.我在 php 中使用这个 class 进行自动加载。 http://pastebin.com/m75f95c3b 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.我有一个名为 foo.class.php 的文件,它找不到 class。 But when i chagne the filename to Foo.class.php it will find the class.但是当我将文件名更改为 Foo.class.php 时,它会找到 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.我正在尝试向我的 class 添加一些功能,以便无论文件名是否以大写字母开头,都可以始终找到该文件。 But so far I haven't scuceeded.但到目前为止我还没有成功。

Anyone?任何人?

If all of your file have lowercase names, apply strtolower to the $className variable value in each method of your class:如果您的所有文件都具有小写名称,请将strtolower应用于 class 的每个方法中的$className变量值:

$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.否则,您将不得不测试使用大写和小写字母编写文件名的 2 个<文件名长度>可能性中的每一个。

Gumbo's solution is the best one and that's what almost everyone uses. Gumbo 的解决方案是最好的,几乎每个人都在使用。 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.当然你可以先检查class名称大写的文件是否存在,如果不存在则检查是否存在小写版本。 That's definitely not better than Gumbo's solution.这绝对不比 Gumbo 的解决方案更好。 After all "your" programmers have to follow some conventions/rules.毕竟“你的”程序员必须遵循一些约定/规则。

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.随着不同命名约定的出现,添加其他条件很容易。

I use an __autoload() function that takes into account several different file naming conventions to provide backwards compatibility with previous developers.我使用 __autoload() function 考虑了几种不同的文件命名约定,以提供与以前开发人员的向后兼容性。

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.对于我的 function,我针对不同的文件扩展名执行此操作,例如 .inc、.class 或 .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'.我会将它放在 searchForClassFile() 方法中,在 else 部分带有注释“找到一个文件”。

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.我没有递归地进入 class 目录寻找正确的文件,而是将 map 将 class 命名到特定位置。 This is a common practice.这是一种常见的做法。 For example, foo_bar is mapped to [CLASS_DIRECTORY]/foo/bar.[EXTENSION] .例如, foo_bar映射到[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:在您的情况下,您必须就如何搜索 class 文件做出设计决定,但要修改您的代码:

} 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.你可以写一个 function 而不是 strtolower(),它只降低第一个字符,或者如果使用 PHP > 5.3.0(未正式发布),则使用 lcfirst() ZC1C425278E68384F1C147A。

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.您可以使用自动加载器扫描目录中的 .php 文件,并使用正则表达式实际查看每个文件的内容以查找“class Foo”等模式。 You will need to cache this, of course, preferably by using an array of the form class => directory.当然,您需要缓存它,最好使用 class => 目录形式的数组。 You also need to make sure you invalidate the cache correctly, eg when there are new.php files in the folder.您还需要确保正确地使缓存无效,例如当文件夹中有 new.php 文件时。

I do this for my website, and it works great.我为我的网站做这个,效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM