为什么 PHP 中的函数和方法不区分大小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2749781/
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
Why are functions and methods in PHP case-insensitive?
提问by user198729
Functions and methods in PHP are case-insensitive as illustrated in the following example.
PHP 中的函数和方法不区分大小写,如下例所示。
function ag()
{
echo '2';
}
Ag();
class test {
function clMe()
{
echo 'hi';
}
}
$instance = new test;
$instance->clme();
But that's the not case with variables. What's the rationale?
但变量不是这种情况。理由是什么?
回答by Shailesh Kumar
Let me quote from Interview – PHP's Creator, Rasmus Lerdorf
让我引用采访 - PHP 的创造者 Rasmus Lerdorf
The first version of PHP was a simple set of tools that I put together for my Website and for a couple of projects. One tool did some fancy hit logging to an mSQL database, another acted as a form data interpreter. I ended up with about 30 different little CGI programs written in C before I got sick of it, and combined all of them into a single C library. I then wrote a very simple parser that would pick tags out of HTML files and replace them with the output of the corresponding functions in the C library.
The simple parser slowly grew to include conditional tags, then loop tags, functions, etc. At no point did I think I was writing a scripting language. I was simply adding a little bit of functionality to the macro replacement parser. I was still writing all my real business logic in C.
PHP 的第一个版本是一组简单的工具,我为我的网站和几个项目组合了这些工具。一个工具对 mSQL 数据库进行了一些奇特的命中记录,另一个充当表单数据解释器。在我厌烦之前,我最终得到了大约 30 个用 C 编写的不同的小 CGI 程序,并将它们组合成一个单一的 C 库。然后我编写了一个非常简单的解析器,它会从 HTML 文件中挑选标签,并用 C 库中相应函数的输出替换它们。
简单的解析器慢慢发展为包含条件标签,然后是循环标签、函数等。我从来不认为我在编写脚本语言。我只是向宏替换解析器添加了一点功能。我仍然在用 C 编写所有真正的业务逻辑。
I have read somewhere that since all the functions introduced essentially felt like tags in an HTML document and since HTML tags were case insensitive, he chose function names in PHP to be case insensitive. Later on this feature remained on in the language.
我在某处读到过,因为引入的所有函数本质上就像 HTML 文档中的标签,并且由于 HTML 标签不区分大小写,所以他在 PHP 中选择了不区分大小写的函数名称。后来这个功能保留在语言中。
回答by Pascal MARTIN
Yes, functions and methods names are not case-sensitive.
是的,函数和方法名称不区分大小写。
And yes, variables names are case-sensitive.
是的,变量名称区分大小写。
I am not sure there's a reason for that -- except it's been this way for a long time, and, so, remains the case, for backward compatibility reasons.
我不确定这是否有原因——除了这种方式已经存在很长时间了,因此,出于向后兼容性的原因,情况仍然如此。
As a reference, a couple of links / quotes to various pages of the manual:
作为参考,有几个链接/引用到手册的各个页面:
For functions (quoting):
对于函数(引用):
Note:Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.
注意:函数名是不区分大小写的,尽管在函数声明中出现时调用函数通常是一种很好的形式。
And methods are not much more than functions in objects -- especially when we think about PHP 4 and backward-compatibility.
并且方法只不过是对象中的函数——尤其是当我们考虑 PHP 4 和向后兼容性时。
And, for variables (quoting):
并且,对于变量(引用):
Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
PHP 中的变量由美元符号后跟变量名称表示。变量名区分大小写。
And object properties are not much more than variables in objects -- same remark about PHP 4 and backward-compatibility.
并且对象属性只不过是对象中的变量——关于 PHP 4 和向后兼容性的相同评论。

