PHP 类名中的大写字母

发布于 2024-10-20 23:59:50 字数 155 浏览 2 评论 0原文

我的系统中有两个类。第一个称为文件,第二个称为文件。 在我的本地主机上,当我实例化文件时,我得到文件对象,但是我的朋友运行相同的脚本获取文件对象,就像大写字母无法识别并且“文件”等于“文件”。 这是一些可配置的东西吗? 我们都在 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 技术交流群。

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

发布评论

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

评论(3

心舞飞扬 2024-10-27 23:59:51

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

检查自动加载的 PHP 手册

以下代码:

<?php

class file {
    public $a;
}

class File {
    public $a2;
}

$x = new file();

给出错误: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:

<?php

class file {
    public $a;
}

class File {
    public $a2;
}

$x = new file();

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 to File it simply assumes that that it has already loaded the class (due to case insensitive behavior of PHP).

新人笑 2024-10-27 23:59:50

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 named File 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.

青巷忧颜 2024-10-27 23:59:50

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

class myclass {}

$x = new MYclaSS;

var_dump($x);

object(myclass)#1 (0) {
}

因此一般建议:您不应该开始并尝试在那里混合某些内容:)

像这样的代码应该工作:

class ab {}

class AB {}

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

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

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 not work:

class ab {}

class AB {}

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