php PHP类名中的大写字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5260168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:47:25  来源:igfitidea点击:

Capital letters in class name PHP

phpclassletters

提问by Somal

I have two classes in my system. One is called file and second is File. On my localhost when i instantiate file i get file object, but my friend running the same script gets object of File like the capital letters were unrecognized and "file" was equal to "File". Is that some configurable thing? We are both running on Windows. I have WampServer, he has XAMPP.

我的系统中有两个类。一个叫做文件,第二个是文件。在我的本地主机上,当我实例化文件时,我得到了文件对象,但是我的朋友运行相同的脚本会得到文件的对象,就像无法识别大写字母一样,“文件”等于“文件”。那是一些可配置的东西吗?我们都在 Windows 上运行。我有 WampServer,他有 XAMPP。

回答by krtek

PHP is case insensitive for the class naming. it means you can normally do $file = new file()even if the class is named Fileand vice-versa.

PHP 的类命名不区分大小写。这意味着$file = new file()即使类被命名File,您通常也可以这样做,反之亦然。

Are you by any chance relying on the auto loading of class files ? If this is the case, it is possible that depending on the computer, the interpreter don't always find the same file first. This will explain the problem.

您是否有机会依赖类文件的自动加载?如果是这种情况,根据计算机的不同,解释器可能并不总是首先找到相同的文件。这将解释问题。

I strongly suggest that you rename your classes. It's always a bad idea to rely on case to differentiate two different things and by convention, class names always start with a capital letter.

我强烈建议你重命名你的类。依靠大小写来区分两个不同的事物总是一个坏主意,按照惯例,类名总是以大写字母开头。

If you can't change the class names, I suggest to have a look at php namespaces.

如果你不能改变类名,我建议看看php namespaces

回答by edorian

Classnames in PHP are not case sensitive (that doesn't depend on the operating system)

PHP 中的类名不区分大小写(不依赖于操作系统)

class myclass {}

$x = new MYclaSS;

var_dump($x);

object(myclass)#1 (0) {
}

so as general advice: You shouldn't start and try to mix something there :)

所以作为一般建议:你不应该开始并尝试在那里混合一些东西:)

Code like this should notwork:

像这样的代码应该工作:

class ab {}

class AB {}

Fatal error: Cannot redeclare class AB in ... on line x

回答by vbence

I guess you are using some kind of lazy loadingfor class files, may be you are programming in a PHP framework. The secret will lie in your __autoloadfunction. Find it.

我猜您正在对类文件使用某种延迟加载,可能是您正在使用 PHP 框架进行编程。秘密就在你的__autoload函数中。找到它。

Check PHP manual for Autoloading.

检查PHP 手册以了解 Autoloading

The following code:

以下代码:

<?php

class file {
    public $a;
}

class File {
    public $a2;
}

$x = new file();

Gives an error: Cannot redeclare class Fileso again, the trick might be which file is included.

给出一个错误:Cannot redeclare class File同样,诀窍可能是包含哪个文件。

Behavior of your code displays that one of the classes isn't being loaded (otherwise you'll see class redeclare error). It is probably the auto loader that first loads the fileclass and then when it finds definition to Fileit simply assumes that that it has already loaded the class (due to case insensitive behavior of PHP).

您的代码行为表明其中一个类没有被加载(否则您会看到class redeclare error)。可能是自动加载器首先加载file类,然后当它找到定义时,File它简单地假设它已经加载了类(由于 PHP 不区分大小写的行为)。