views:

639

answers:

6

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:

class Foo
{
   function foo()
   {

   }   
}

or :

class Foo
{
   function Foo()
   {

   }   
}

Which is a better convention? I'm confused.

+5  A: 

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.

John Kugelman
Well, Foo is the old-style constructor name. The new style uses __construct. But you're still right :)
lavinio
Good point. In my world PHP 4 is the "new" version of PHP. I'm a little behind the times. :-P
John Kugelman
+2  A: 

Although this is the convention in many language, such as Java or C#, it is not so in all.

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.

Java also uses Capitalized Class Names, but methods within a class are camelCased.

C# of course, confuses things by using Capitalized Class Names and the same for Methods.

lavinio
php although i'd also like to know what is used in java
Click Upvote
+1  A: 

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.)

For other languages, use the same naming convention you use for the rest of the class's methods. It's just that simple.

Rob Kennedy
+2  A: 

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.

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).
lc
+10  A: 

Named constructors are PHP 4 convention. And yes, when you do it that way you should match the case of the class.

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.

class a
{
  public function __construct()
  {}
}

class b
{
  public function __construct()
  {}
}
Peter Bailey
Faster by one minute!
jason
I don't like using __construct. Its fricking ugly. The constructor method is also used in java so I like that . What's the advantage of __construct?
Click Upvote
Do I understand this right? You're using PHP 5, but deliberately write PHP 4 style constructors, because you think the PHP 5 style ones are "ugly"?
Peter Bailey
One advantage is that support for the PHP4, Java-style constructors will be gone in PHP 6.
jason
Php 6 will never be adopted mainstream in that case and it won't be a problem. Already php 5's adoption rate is low. I'm both a PHP and Java coder so I prefer to use the same method for both..
Click Upvote
+4  A: 

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.

jason