Thread: Best practices

    #1
  1. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115

    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
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    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.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    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
  6. #4
  7. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    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.
  8. #5
  9. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    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...
  10. #6
  11. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    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 eZContentClass...
    Cheers! I'll definitely try be consistent. I pass the Hungarian notation though :P
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Ithaca
    Posts
    68
    Rep Power
    2
    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.
  14. #8
  15. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    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.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  16. #9
  17. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    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.
    PHP Code:
    if(){
      
    //bla

    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
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Checkout PHP_CodeSniffer. It may help you to be consistent in your code conventions
  20. #11
  21. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by rstoll
    Checkout PHP_CodeSniffer. It may help you to be consistent in your code conventions
    cheers I'll check it out!

IMN logo majestic logo threadwatch logo seochat tools logo