Best practices

Hi guys,

I am making an attempt on learning OOP. Now I thought before I go ahead, does anyone know some best practices. like standards for variable, class and function(method) naming/notation. I have seen quite a few like camel notation and all, but not sure if anyone knows or could point me to a sort of W3C-like recommendations for writing. If anyone has tips or hint let me know

There are no such standards, thankfully. You are free to see the various standards, like Zend's or that one Github (?) project that nominated itself as The Standard To Rule Them All, and adopt the ones or parts of that you like most.

I'm not aware of any "official" code style standard. There are standards for particular frameworks and applications like PEAR or the Zend framework. But PHP itself doesn't seem to have specific naming rules -- which is not surprising, since the internal names themselves are a wild mixture of different traditions.

I'd simply use common sense (names should be descriptive etc.) and the following basic rules:

class names start with an uppercase letter and use CamelCase

constants are all-uppercase

methods start with a lowercase letter and either use camelCase or the underscore_notation

Whatever you choose, the absolute most important thing is consistency. I don't care if you elect to use Hungarian notation so long as you use it consistently. Very few things piss me off more than not knowing whether a class is called, say, ezContentClass or eZContentClass...

Whatever you choose, the absolute most important thing is consistency. I don't care if you elect to use Hungarian notation so long as you use it consistently. Very few things piss me off more than not knowing whether a class is called, say, ezContentClass or eZContentClass...

Cheers! I'll definitely try be consistent. I pass the Hungarian notation though :P

Yeah, the best practice is to be consistent with one coding style throughout your project. Also if you wish to use a framework like Zend, Symfony, CakePHP and Codeigniter, its a good practice to follow their standards rather than mixing it up with your own.

A general trend in the PHP core classes seems to be using underscore notation for functions, and camel case notation for classes and methods. However, as Jacques1 mentioned the core code has a lot of inconsistencies.

I think one of the only standards that is pretty much universally agreed upon is the use of all uppercase for constants. This is pretty much even consistent across programming languages, not just PHP.

To be honest, I would prefer that PHP did have required standards. I work with a lot of diverse code bases on a daily basis and it is honestly extremely annoying to have to switch coding styles every 2 hours because every single project makes up its own.

Consistency is the most important part though, and I agree with that Hall Of Famer said regarding matching the styles of the code you're working on.

Personally I prefer the following:

all identifiers (variable names, function names, class names, class properties, method names, build-in values) except constants are all lowercase with underscores between words
I find it easier to read than camel case. It is syntactically impossible in PHP to not know the difference between a variable name, a function name, a class name and a method name based solely on its context in the code (although this is not true in all languages), so there is no reason to distinguish between them using different styles. This way, it is incredibly simple to remember what format to use for any given identifier and you never accidentally use the wrong format for an identifier, because all identifiers use the same format.

Also, if you name your files according to the code they contain, like most projects do, then you end up with all lowercase file names. This is particularly beneficial when working cross platform, because not all file systems are case sensitive and some version control systems (SVN) have a notoriously bad history of handling mixed filename case.

except constants, because constants are always in uppercase and use underscores to separate words

never use one-character variable names, even for loop count variables
They are a pain in the *** to find/replace on, and that is no exception for loop count variables.

never omit optional curly braces around control statements
You are less likely to make a reading error if you have the braces there. Plus it's a lot easier to go back and add/remove debugging statements. Plus it really does not take long to type two characters.

never use short tags

always indent using hard tabs

I drop my starting and closing brackets to the next line for everything