Re:Ask HN: PHP Tricks and Best Practices?
by ericwaller on Wednesday October 29, 2008 @11:27PM
(#348384)
I definitely prefer the ruby build_tag too, php's fine.. until you try to do something even remotely interesting.
And yeah, in real code the helpers sometimes need to get a bit more advanced. Something I like to do is add a class name corresponding to the function name (which I usually grab from __FUNCTION__) -- it helps keep a nice consistency between server-side/html/js/css
At any rate, a downside to this approach is that you can't easily add a class or target (or any other attribute) to the A tag (at least in your simplified example).
Of course you could have an optional named parameter that would allow you to pass them in, e.g.:
But trying to do method-chainey sorts of things in PHP quickly runs into the language's clunky syntax, not to mention keeping track of exactly what level of quotation you're at, and so on. This is one of the reasons I like Ruby, as this kind of stuff is much more natural. Something like this: Both of these are probably a little denser than a sane person would put into production code, but the Ruby example is much more human-parseable to my eyes. (I'm using rails helper 'h', which could easily be defined in PHP as well).