{
    "href": "/post/2010/10/19/regarding-underscores/",
    "relId": "2010/10/19/regarding-underscores",
    "title": "Regarding Underscores",
    "author": "pmjones",
    "markup": "html",
    "tags": [
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/programming/",
            "relId": "programming",
            "title": "Programming",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        }
    ],
    "created": "2010-10-19 13:50:18 UTC",
    "updated": [
        "2010-10-19 13:50:18 UTC"
    ],
    "html": "<p>Today, <a href=\"http://www.phpdeveloper.org/news/15297\">PHPDeveloper.org</a> referred to a post by Leszek Stachowski about <a href=\"http://shazarre.wordpress.com/2010/10/18/php-anachronic-coding-standards/\">underscore prefixes on non-public class elements</a>.</p>\n<blockquote>\n<p>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!</p>\n<p>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.</p>\n</blockquote>\n<p>I think the underscore-prefix for protected and private is a good convention to keep. As with many things about programming, this convention is not for the <em>program</em>, it is for for the <em>programmer</em>.  For example, <a href=\"http://paul-m-jones.com/archives/276\">limiting your line length to about 80 characters</a> is a good idea, not for reasons of \u201ctradition\u201d, but because of cognitive limitations of human beings who have to read code.</p>\n<p>Likewise, using the underscore prefix is an immediate and continuous reminder that some portions of the code are not public. Its purpose is as an aid to the programmer. The underscores make it obvious which parts of the program are internal, and which parts are externally available.  (Note that I do not extend this argument to support the use of Hungarian Notation in PHP; if something like the underscore prefix is overused, it loses its obvious-ness and thus becomes less powerful.)</p>\n<p>As an example, look at the following code:</p>\n<pre><code>&lt;?php\nclass NoUnderscores\n{\n    protected $data = array(\n        'item' =&gt; 'magic-data',\n    );\n\n    protected $item = 'property-value';\n\n    public function __get($key)\n    {\n        return $this-&gt;data[$key];\n    }\n\n    protected function doSomething()\n    {\n        // do we want the magic public item,\n        // or the internal protected item?\n        return $this-&gt;item;\n    }\n}\n</code></pre>\n<p>Here we have magic <code>__get()</code> method that reads from the protected $data property. Any time you try to access a property that doesn\u2019t exist, PHP will go to the <code>__get()</code> method and read from protected <code>$data</code>. Now look in the <code>doSomething()</code> method. Because the code executes inside the class, it has access ot the protected <code>$item</code>, so it\u2019s not obvious if the programmer wanted the value of protected <code>$item</code>, or the magic <code>$data['item']</code>.</p>\n<p>By way of comparison, take a look at the following modification to use the underscore prefix on private and protected elements:</p>\n<pre><code>&lt;?php\nclass Underscores\n{\n    protected $_data = array(\n        'item' =&gt; 'magic-data',\n    );\n\n    protected $_item = 'property-value';\n\n    public function __get($key)\n    {\n        return $this-&gt;_data[$key];\n    }\n\n    protected function _doSomething()\n    {\n        // it is clear we want the internal protected item\n        return $this-&gt;_item;\n    }\n}\n</code></pre>\n<p>Now the <code>_doSomething()</code> method is perfectly clear: the programmer wants the value of the internal protected property.</p>\n"
}
