December 4th, 2012, 12:40 PM
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
December 4th, 2012, 01:42 PM
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.
December 4th, 2012, 01:43 PM
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
- the same with attributes
December 4th, 2012, 01:50 PM
Thanks both of you for the links and advice. I'll have a look at it
P.s. I asked because the tutorial i do doesn't seem very consequent but its fun.
p.p.s funny comic about it
Last edited by aeternus; December 4th, 2012 at 01:57 PM.
December 4th, 2012, 02:12 PM
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...
December 4th, 2012, 02:16 PM
Cheers! I'll definitely try be consistent. I pass the Hungarian notation though :P
Originally Posted by requinix
December 4th, 2012, 05:47 PM
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.
December 4th, 2012, 08:06 PM
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
It's a lot easier to drop/add braces when you can do it by manipulating the entire line.
spacing: I add a space between parameters and operators (except !), but nowhere else
No particular reason for this, it's just how I do it.
function(param1, param2, param3);
if($var == 'value')
Anyway; obviously many people will disagree and have different styles, but those are examples of things to consider as you develop your own unique standard.
December 5th, 2012, 02:20 PM
Thanks Alot for the time you took E-Oreo. I see some things in your method I do already. Except for the curly braces. for some reason I do.
Originally Posted by E-Oreo
I will make sure I will be consistent and not invent a $_CHa_oS__nOT_at_ION
Cheers guys, hopefully I'll soon master OOP
December 10th, 2012, 06:52 PM
Checkout PHP_CodeSniffer. It may help you to be consistent in your code conventions
December 10th, 2012, 06:56 PM
cheers I'll check it out!
Originally Posted by rstoll