The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-OOP - Best practices
Discuss Best practices in the PHP Development forum on Dev Shed. Best practices PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 4th, 2012, 11:40 AM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
|
PHP-OOP - 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 
|

December 4th, 2012, 12:42 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
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, 12:43 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
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, 12:50 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
Thanks both of you for the links and advice. I'll have a look at it
Cheers!
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 12:57 PM.
|

December 4th, 2012, 01:12 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
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 e ZContentClass...
|

December 4th, 2012, 01:16 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
Quote: | Originally Posted by requinix 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 e ZContentClass... |
Cheers! I'll definitely try be consistent. I pass the Hungarian notation though :P
|

December 4th, 2012, 04:47 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Location: Ithaca
Posts: 64
Time spent in forums: 13 h 7 m 48 sec
Reputation Power: 1
|
|
|
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, 07:06 PM
|
 |
Lost in code
|
|
|
|
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
Code:
if(condition)
{
code
}
switch(condition)
{
case: xyz;
code
break;
}
function xyz()
{
code
}
class xyz
{
code
}
etc.
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
Code:
function(param1, param2, param3);
if($var == 'value')
if(!$var)
No particular reason for this, it's just how I do it.
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, 01:20 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
Quote: | Originally Posted by E-Oreo A general trend in the PHP [....] |
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.
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, 05:52 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 2 h 3 m
Reputation Power: 0
|
|
|
Checkout PHP_CodeSniffer. It may help you to be consistent in your code conventions
|

December 10th, 2012, 05:56 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
Quote: | Originally Posted by rstoll Checkout PHP_CodeSniffer. It may help you to be consistent in your code conventions |
cheers I'll check it out!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|