I use camel case which has the first letter of all variable names, functions, etc lowercased. But the class names are capitalized. Should I use:
我使用camel case,它的所有变量名,函数等的第一个字母都是小写的。但是班级名称是大写的。我应该使用:
class Foo
{
function foo()
{
}
}
or :
class Foo
{
function Foo()
{
}
}
Which is a better convention? I'm confused.
哪个更好的约定?我糊涂了。
6 个解决方案
#1
Named constructors are PHP 4 convention. And yes, when you do it that way you should match the case of the class.
命名构造函数是PHP 4约定。是的,当你这样做时,你应该匹配类的情况。
But PHP 4 is old news these days. PHP 5 has been the de-facto version for several years now. And PHP 5 uses a constant constructor name, __construct. It's always this, regardless of the class' name.
但PHP 4现在是旧闻。 PHP 5几年来一直是事实上的版本。 PHP 5使用常量构造函数名称__construct。无论班级名称如何,总是如此。
class a
{
public function __construct()
{}
}
class b
{
public function __construct()
{}
}
#2
Honestly, neither.
You should be using __construct()
Unless, of course, you're chained to the rather archaic PHP4, then you exactly match the name of the class.
你应该使用__construct()当然,除非你被链接到相当古老的PHP4,否则你完全匹配类的名称。
#3
The constructor name should match the class name, so Foo
. That's the convention, not only in PHP but in the languages it is copying: Java, C#, and C++. In those languages identifiers are case sensitive so the name must be identical.
构造函数名称应该与类名匹配,所以Foo。这是惯例,不仅在PHP中,而且在它复制的语言中:Java,C#和C ++。在这些语言中,标识符区分大小写,因此名称必须相同。
#4
If at all possible, I would go with the second example. That is, the constructor's case should match the class name, because the constructor is returning an instance of the class so it should be named the same (for consistency).
如果可能的话,我会选择第二个例子。也就是说,构造函数的大小写应与类名匹配,因为构造函数正在返回类的实例,因此它的名称应该相同(为了保持一致性)。
This may be personal convention, but if I have a class "FooBarBaz", I expect the constructor to be named "FooBarBaz", not "fOOBARBAZ", "fooBarBaz", "foobarbaz" or anything else.
这可能是个人约定,但如果我有一个类“FooBarBaz”,我希望构造函数名为“FooBarBaz”,而不是“fOOBARBAZ”,“fooBarBaz”,“foobarbaz”或其他任何东西。
However, it is language-dependent:
但是,它依赖于语言:
- Some languages are case-sensitive, thus they force this behavior and will error out if the casing is different.
- Some languages are case-insensitive and don't care.
- I'm not sure, but some languages might force you to name the constructor differently (not sure how the compiler recognizes it as a constructor, but that's not the point).
某些语言区分大小写,因此它们会强制执行此行为,并且如果大小写不同则会出错。
有些语言不区分大小写并且不关心。
我不确定,但有些语言可能会强迫您以不同的方式命名构造函数(不确定编译器如何将其识别为构造函数,但这不是重点)。
#5
Although this is the convention in many language, such as Java or C#, it is not so in all.
虽然这是许多语言的惯例,例如Java或C#,但并非如此。
Capitalization is dependent on the language or class library conventions in your development context.
大写取决于开发上下文中的语言或类库约定。
So, what language are we talking about?
那么,我们在谈论什么语言?
Okay, so PHP. The convention in PHP is to Capitalize Class Names.
好的,所以PHP。 PHP中的约定是大写类名。
Java also uses Capitalized Class Names, but methods within a class are camelCased.
Java也使用大写的类名,但类中的方法是camelCased。
C# of course, confuses things by using Capitalized Class Names and the same for Methods.
当然,C#使用大写的类名来混淆事物,而对于方法则相同。
#6
Many languages don't give you a choice. That includes, C++, Java, and C#. The name of the constructor is the name of the class, so capitalize the constructor name if and only if you capitalized the class name. (If you don't, then the compiler won't even recognize it as a constructor anyway.)
许多语言都没有给你一个选择。这包括C ++,Java和C#。构造函数的名称是类的名称,因此当且仅当您将类名称大写时,才将大写构造函数名称大写。 (如果不这样做,那么编译器甚至都不会将它识别为构造函数。)
For other languages, use the same naming convention you use for the rest of the class's methods. It's just that simple.
对于其他语言,请使用与该类其余方法相同的命名约定。就是这么简单。
#1
Named constructors are PHP 4 convention. And yes, when you do it that way you should match the case of the class.
命名构造函数是PHP 4约定。是的,当你这样做时,你应该匹配类的情况。
But PHP 4 is old news these days. PHP 5 has been the de-facto version for several years now. And PHP 5 uses a constant constructor name, __construct. It's always this, regardless of the class' name.
但PHP 4现在是旧闻。 PHP 5几年来一直是事实上的版本。 PHP 5使用常量构造函数名称__construct。无论班级名称如何,总是如此。
class a
{
public function __construct()
{}
}
class b
{
public function __construct()
{}
}
#2
Honestly, neither.
You should be using __construct()
Unless, of course, you're chained to the rather archaic PHP4, then you exactly match the name of the class.
你应该使用__construct()当然,除非你被链接到相当古老的PHP4,否则你完全匹配类的名称。
#3
The constructor name should match the class name, so Foo
. That's the convention, not only in PHP but in the languages it is copying: Java, C#, and C++. In those languages identifiers are case sensitive so the name must be identical.
构造函数名称应该与类名匹配,所以Foo。这是惯例,不仅在PHP中,而且在它复制的语言中:Java,C#和C ++。在这些语言中,标识符区分大小写,因此名称必须相同。
#4
If at all possible, I would go with the second example. That is, the constructor's case should match the class name, because the constructor is returning an instance of the class so it should be named the same (for consistency).
如果可能的话,我会选择第二个例子。也就是说,构造函数的大小写应与类名匹配,因为构造函数正在返回类的实例,因此它的名称应该相同(为了保持一致性)。
This may be personal convention, but if I have a class "FooBarBaz", I expect the constructor to be named "FooBarBaz", not "fOOBARBAZ", "fooBarBaz", "foobarbaz" or anything else.
这可能是个人约定,但如果我有一个类“FooBarBaz”,我希望构造函数名为“FooBarBaz”,而不是“fOOBARBAZ”,“fooBarBaz”,“foobarbaz”或其他任何东西。
However, it is language-dependent:
但是,它依赖于语言:
- Some languages are case-sensitive, thus they force this behavior and will error out if the casing is different.
- Some languages are case-insensitive and don't care.
- I'm not sure, but some languages might force you to name the constructor differently (not sure how the compiler recognizes it as a constructor, but that's not the point).
某些语言区分大小写,因此它们会强制执行此行为,并且如果大小写不同则会出错。
有些语言不区分大小写并且不关心。
我不确定,但有些语言可能会强迫您以不同的方式命名构造函数(不确定编译器如何将其识别为构造函数,但这不是重点)。
#5
Although this is the convention in many language, such as Java or C#, it is not so in all.
虽然这是许多语言的惯例,例如Java或C#,但并非如此。
Capitalization is dependent on the language or class library conventions in your development context.
大写取决于开发上下文中的语言或类库约定。
So, what language are we talking about?
那么,我们在谈论什么语言?
Okay, so PHP. The convention in PHP is to Capitalize Class Names.
好的,所以PHP。 PHP中的约定是大写类名。
Java also uses Capitalized Class Names, but methods within a class are camelCased.
Java也使用大写的类名,但类中的方法是camelCased。
C# of course, confuses things by using Capitalized Class Names and the same for Methods.
当然,C#使用大写的类名来混淆事物,而对于方法则相同。
#6
Many languages don't give you a choice. That includes, C++, Java, and C#. The name of the constructor is the name of the class, so capitalize the constructor name if and only if you capitalized the class name. (If you don't, then the compiler won't even recognize it as a constructor anyway.)
许多语言都没有给你一个选择。这包括C ++,Java和C#。构造函数的名称是类的名称,因此当且仅当您将类名称大写时,才将大写构造函数名称大写。 (如果不这样做,那么编译器甚至都不会将它识别为构造函数。)
For other languages, use the same naming convention you use for the rest of the class's methods. It's just that simple.
对于其他语言,请使用与该类其余方法相同的命名约定。就是这么简单。