web developer's blog

php anachronic coding standards

with 15 comments

When you take a look at the primary PHP coding standards, like PEAR or Zend, there’s what you can find there:

Private class members are preceded by a single underscore.

or:

For methods on objects that are declared with the “private” or “protected” modifier, the first character of the method name must be an underscore. This is the only acceptable application of an underscore in a method name. Methods declared “public” should never contain an underscore.

The question which comes instantly to my mind is: why? Is there any reason why this convention should be kept when PHP object oriented programming has gone a long way since PHP 4 (when there was no access modifiers and such underscore was the only fast way to distinguish public from, hmm, not public methods and properties) ? Are, for instance (as one of major OOP languages), Java coding standards pushing towards such naming convention? No!

I think that we, as developers, should not stick to this silly convention. For the sake of progress, stop looking back (because that what in fact this convention is) and  stop supporting this one, particular naming convention.

Written by Leszek Stachowski

October 18, 2010 at 12:48 am

Posted in PHP

Tagged with , , , ,

15 Responses

Subscribe to comments with RSS.

  1. […] a new post to his blog Leszek Stachowski wonders about a coding convention that’s still widely used when it comes to private methods in a class – the underscore prefix. […]

  2. You’re basically right.

    However, in terms of “official” PHP coding standards, I think you might want ot take a closer look at one of the older (and better!) PROPOSED standards:
    http://www.horde.org/horde/docs/?f=CODING_STANDARDS.html

    …the 5-page stuff you link to cold be made by anybody within 5 minutes…

    Michael

    October 18, 2010 at 9:57 pm

  3. I agree that thoses underscores are a bit ugly. But as everybody know it including you, that’s mean that you would quickly understand my code (if i respect this huh?). Otherwise it would take much more effort to get into it, right ?
    Maybe it’s time to mix differents conventions to evolve to a better one ? I would agree 🙂 But will thoses old daddy accept to change the rules ?

    Nicolas BUI

    October 18, 2010 at 10:28 pm

    • Right, I think that most of us has very own coding convention, because if I don’t apply absolutely all PEAR conventions then I don’t follow the standard in general. Standard (as a set of conventions) is – for me – either followed or not if only a single convention is rejected – then it is only *based* on base standard.

      shazarre

      November 5, 2010 at 7:05 pm

  4. I am a Java developer who uses PHP just for fun. I was asking myself the same questions a while ago. Watching some conversations on the PHP mailing list and discussions on IRC my conclusion is that PHP community is very slow to make any progress towards the future – comparing to other modern languages. Keep it simple, stupid principle is applied almost on each occasion. I think this is what makes the language specific and limits it in the same time.

    Daniel Stefaniuk

    October 18, 2010 at 10:29 pm

  5. Scoping modifiers are great. But having that little reminder later in the code that something is or is not private is not a bad thing, especially since the code will flow thru many hands over time. Private or protected appears once in the block of code where the variable is declared… $_theVar appears every single use and the context in which it is being used is very clear wherever you see it.

    It is not a step back, it is a method that keeps your head screwed on straight. This is not about keeping up with the time, it’s about maintaining what can be pieces of legacy code over time across multiple people’s hands and minds. Nothing is worse than checking out a hunk of source that someone has peppered with code written to their own standards instead of those with which it was conceived.

    Baron

    October 19, 2010 at 6:49 am

  6. Good point, the only time I find this convention barely useful is when using some metaprogramming, to detect if a __get, __set or __call has been called to access an otherwise hidden attribute/method.

    Olek

    October 19, 2010 at 9:58 am

  7. We use underscores in property naming to identify what we call configuration properties, properties that are intended to be overwritten in subclasses. Only protected properties have this possibility.

    We use these properties often in default view classes that generate HTML, for class and id naming, for example.

    Pim

    October 19, 2010 at 10:55 am

  8. Underscores should remind to not touch private attributes, but create getter/setter 😉 In fact it claryfies the code, and makes quick review, telling how underscored methods are called in other places.

    wowo

    October 19, 2010 at 2:27 pm

  9. I hate underscores but I can see why they are still necessary. There are a lot of php developers who use editors like vim that do not offer code completion by default. When scanning through code for method names it is a lot easier to find the public api of a class.

    Then again, there are a lot of non-php developers who use editors like vim and they don’t seem to prefix items with an underscore…

    Josh Johnston

    October 19, 2010 at 3:19 pm

  10. […] Today, PHPDeveloper.org referred to a post by Leszek Stachowski about underscore prefixes on non-public class elements. […]

  11. If you are aspiring for Java-style coding in PHP, then you squarely hit the definition of cargo cult software engineering.

    PHP5 is not a thouroughly object-oriented language by any measurement. Python is. And Python uses underscores in lieu of access modifiers. And imagine that, it worked out well. You only need syntactic sugar like “private” and “protected” if you are SIMULATING Java. It doesn’t help that you’ll get access violations at runtime instead of at compile time like with Java. And lastly you really should have a factual use case beyond defensive/paranoid coding and the encapsulation buzzword.

    mario

    October 19, 2010 at 6:59 pm

    • Just as you wrote, python uses it in lieu of access modifiers, so you don’t mix those two things. An underscore doesn’t automatically imply a private access in PHP, so I think you don’t have the point here.

      I agree that PHP5 is not a thoroughly OO language, but if it does offer this paradigm, why don’t follow the best examples which (for example of course) Java has to offer? Especially regarding all the effort that PHP language creators have made and how is OO currently implemented.

      shazarre

      November 5, 2010 at 7:21 pm

  12. shouldn’t the editor you use *dynamically* provide hints about the status of that function/method, instead of hardcoding that status in the name of the function, as the status could change (and then you have to change the name everywhere it is used!)?

    à la “:before” and “:after” css rules.

    pure text editor (B&W screen): underscore prefix *dynamically* inserted for private (but not selectable for copy-paste and so on)

    graphical editor: red/bold for private, orange/italic for protected, green/normal text for public, or a little trafficlight, or …

    it then ables you to distinguish between content (the code) and presentation (hints about status).

    eleg

    October 20, 2010 at 7:50 am

  13. […] Horde Coding Standards (via web developer’s blog) […]


Leave a reply to Olek Cancel reply