PHP 类名中的大写字母
我的系统中有两个类。第一个称为文件,第二个称为文件。 在我的本地主机上,当我实例化文件时,我得到文件对象,但是我的朋友运行相同的脚本获取文件对象,就像大写字母无法识别并且“文件”等于“文件”。 这是一些可配置的东西吗? 我们都在 Windows 上运行。我有WampServer,他有XAMPP。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜您正在对类文件使用某种延迟加载,可能您正在 PHP 框架中进行编程。秘密在于您的 __autoload 函数。找到它。
检查自动加载的 PHP 手册。
以下代码:
给出错误:
Cannot redeclare class File
因此,技巧可能是包含哪个文件。代码的行为显示其中一个类尚未加载(否则您将看到类重新声明错误)。它可能是自动加载器首先加载
file
类,然后当它找到File
的定义时,它只是假设它已经加载了该类(由于不区分大小写) PHP 的行为)。I guess you are using some kind of lazy loading for class files, may be you are programming in a PHP framework. The secret will lie in your
__autoload
function. Find it.Check PHP manual for Autoloading.
The following code:
Gives an error:
Cannot redeclare class File
so again, the trick might be which file is included.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
file
class and then when it finds definition toFile
it simply assumes that that it has already loaded the class (due to case insensitive behavior of PHP).PHP 的类命名不区分大小写。这意味着即使该类名为
File
,您通常也可以执行$file = new file()
,反之亦然。您是否有机会依赖类文件的自动加载?如果是这种情况,根据计算机的不同,解释器可能并不总是首先找到相同的文件。这样就可以解释问题了。
我强烈建议您重命名您的课程。依靠大小写来区分两个不同的事物总是一个坏主意,并且按照惯例,类名始终以大写字母开头。
如果您无法更改类名,我建议您查看 php 命名空间。
PHP is case insensitive for the class naming. it means you can normally do
$file = new file()
even if the class is namedFile
and vice-versa.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 中的类名不区分大小写(不依赖于操作系统),
因此一般建议:您不应该开始并尝试在那里混合某些内容:)
像这样的代码应该不工作:
Classnames in PHP are not case sensitive (that doesn't depend on the operating system)
so as general advice: You shouldn't start and try to mix something there :)
Code like this should not work: