{
    "href": "/post/2010/07/09/universal-constructor-sighting-in-the-wild/",
    "relId": "2010/07/09/universal-constructor-sighting-in-the-wild",
    "title": "Universal Constructor Sighting \"In The Wild\"",
    "author": "pmjones",
    "markup": "html",
    "tags": [
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        }
    ],
    "created": "2010-07-09 13:30:54 UTC",
    "updated": [
        "2010-07-09 13:30:54 UTC"
    ],
    "html": "<p>For those of you who don't know, \"<a href=\"http://solarphp.com/manual/appendix-standards.constructor\">universal constructor</a>\" is the name I give to PHP constructors that always and only take a single parameter.  The parameter is an array of key-value pairs, which is then merged with a set of default keys and values. Finally, the array is unmarshalled, usually into object properties.</p>\n<p>One benefit of the universal constructor is that it allows you to quickly and easily pass in configuration values from a config file (or other source) when building an object.  You don't have to remember the order of parameters, and you only need to specify the values that override the defaults.</p>\n<p>I standardized on a universal constructor in the <a href=\"http://solarphp.com\">Solar framework for PHP</a>.  As far as I know, Solar was the first to standardize on this pattern and give it a name, and other PHP projects appear to be adopting the idea based on my advocacy.   I saw a link today to a universal constructor \"in the wild\", not the result of my direct advocacy, here: <a href=\"http://www.jqueryin.com/projects/mongo-session/\">http://www.jqueryin.com/projects/mongo-session/</a>.</p>\n<p>It's nice to see the idea is getting around.</p>\n"
}
