PHP 函数是否区分大小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5643496/
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
Are PHP functions case sensitive?
提问by Ben Dauphinee
I was digging through some code, and I found some calls to mySQL_fetch_array
. Is PHP case sensitive about function names? I recall reading this somewhere but can't seem to find any reference to it.
我正在挖掘一些代码,我发现了一些对mySQL_fetch_array
. PHP 对函数名是否区分大小写?我记得在某处读过这个,但似乎找不到任何参考。
采纳答案by SIFE
I am quoting from this:
我从这里引用:
Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.
注意:函数名是不区分大小写的,尽管在函数声明中出现时调用函数通常是一种很好的形式。
So, its looks like user-defined functions are not case-sensitive, there was a votefor making functions/objects under PHP5 case-sensitive.
因此,看起来用户定义的函数不区分大小写,有人投票支持使 PHP5 下的函数/对象区分大小写。
回答by mpen
No.
不。
PHP functions are notcase sensitive.
PHP 函数不区分大小写。
回答by Carlos Campderrós
TL;DR: class names are case-insensitive, but use always the same case as in the declaration (same as with functions). Also, instantiating classes with different case as they were defined may cause problems with autoloaders.
TL;DR:类名不区分大小写,但始终使用与声明中相同的大小写(与函数相同)。此外,实例化具有不同大小写的类可能会导致自动加载器出现问题。
Also, class names are case-insensitive:
此外,类名不区分大小写:
<?php
class SomeThing {
public $x = 'foo';
}
$a = new SomeThing();
$b = new something();
$c = new sOmEtHING();
var_dump($a, $b, $c);
This outputs:
这输出:
class SomeThing#1 (1) {
public $x =>
string(3) "foo"
}
class SomeThing#2 (1) {
public $x =>
string(3) "foo"
}
class SomeThing#3 (1) {
public $x =>
string(3) "foo"
}
Problem is using autoloaders and case-sensitive file-systems (like ext2/3/4), in that you must call the class name with the same case the file containing the class is named (not how the class name is actually cased), or use strtolower
:
问题在于使用自动加载器和区分大小写的文件系统(如 ext2/3/4),因为您必须使用包含类的文件命名的相同大小写来调用类名(而不是类名的实际大小写方式),或使用strtolower
:
The class file:
类文件:
<?php
// filename something.php
class SomeThing {
...
}
The autoloader function (__autoload
or a function to register with spl_autoload_register
)
自动加载器函数(__autoload
或要注册的函数spl_autoload_register
)
function my_autloader($className) {
$filename = CLASSES_DIR . DIRECTORY_SEPARATOR . $className . '.php';
if (file_exists($filename)) {
require($filename);
}
}
Now with this code:
现在有了这个代码:
$a = new something(); // works
$b = new SomeThing(); // does not work
$c = new SOMETHING(); // does not work
You may made this work (ie. having effectively case insensitive class names using an autoloader) if you added a call to strtolower()
in the autoloader code, but as with functions, is just better to reference a class in the same way as it is declared, have the filename with the same case as the class name, use autoloaders, and forget using strtolower
and the likes.
如果您strtolower()
在自动加载器代码中添加了对 的调用,您可以完成这项工作(即使用自动加载器有效地使类名不区分大小写),但与函数一样,最好以与声明相同的方式引用类,使用与类名相同大小写的文件名,使用自动加载器,忘记使用strtolower
等等。
回答by alex
No, they are notcase sensitive, however, you should always use the case that is in the manual, for consistency.
不,它们不区分大小写,但是,为了保持一致性,您应该始终使用手册中的大小写。
However, variables arecase sensitive.
然而,变量是区分大小写的。
回答by atif
In PHP variables are case sensitive but functions have no issue like this.You can use following statements for displaying output, all will show the same result.
在 PHP 中变量区分大小写,但函数没有这样的问题。您可以使用以下语句来显示输出,都将显示相同的结果。
<?php
Echo "This is a test script";
ECHO "This is a test script";
echo "This is a test script";
?>
On the other hand, if you will change the case sensitivity of variables then it will show the error.
另一方面,如果您要更改变量的区分大小写,则会显示错误。
Example:
例子:
<?php
$a=5;
echo $A;// It will show the error.
?>
Output:
输出:
Notice: Undefined variable: A in C:\xampp\htdocs\test.php on line 3
回答by Habeeb Perwad
And method names also case-insensitive. eg:-
方法名称也不区分大小写。例如:-
<?php
class C {
public function method() { }
public function METHOD() { }
}
output:
输出:
PHP Fatal error: Cannot redeclare C::METHOD() in ....php on line 6
回答by weia design
In conclusion of everyone's response. Even though PHP does not require character case consistency in all instances even till now in PHP5.
总结一下大家的回答。即使直到现在在 PHP5 中,PHP 也不要求在所有情况下都保持字符大小写一致性。
Best practice will be
最佳实践将是
always use same cases when reference back to either variables(its' mandatory) or functions(its' optional, but recommended).
当引用回变量(它的'强制)或函数(它的'可选,但推荐)时,总是使用相同的情况。
You never know maybe one day the vote get through and you will save the whole nightmare of changing cases in your applications made couple of years ago that require update in PHP.
您永远不知道也许有一天投票会通过,您将避免几年前在需要更新 PHP 的应用程序中更改案例的整个噩梦。
Hope it helps in anyway.
希望它无论如何都有帮助。
回答by Vsevolod Azovsky
May be this is too late, but...
可能为时已晚,但是...
Everyone has already known here: PHP engine does not care about letters' case.
这里大家都知道:PHP引擎不关心字母的大小写。
And there is the vote on PHP bugtracker, where majority says: "Yes, I am pro case sensetivity".
还有对 PHP bugtracker 的投票,大多数人说:“是的,我支持案例敏感度”。
But I am contra, because in my MVC framework, I call controller action:
但我反对,因为在我的 MVC 框架中,我调用控制器操作:
$currentController = new $controller($parameters);
$currentAction = $currentController->{$action}();
$controller and $action are taken from URL as is.
$controller 和 $action 是从 URL 中获取的。
Think, if a user publishes a link to my site: https://my-site/MyPartnerController/MyPartnerGallery
想一想,如果用户发布指向我网站的链接: https://my-site/MyPartnerController/MyPartnerGallery
while class is named myPartnerController...
而类被命名为 myPartnerController...
It means noone ever gains this page if PHP's classes and functions names are case sensitive.
这意味着如果 PHP 的类和函数名称区分大小写,则没有人会获得此页面。
Yes, I always use all the names in code as declared. But I pray they never make functions' and classes' names case sensitive.
是的,我总是使用声明的代码中的所有名称。但我祈祷他们永远不会让函数和类的名称区分大小写。
Thank you!
谢谢!