Okay, so I made this nice new Html class lately to mostly replace Xml
for HTML output. One of the major purposes of Xml, and now one of the
major purposes of Html, is to provide convenient wrapper functions for
particular elements that are more concise than Html::element( 'foo',
array( 'x' => 'y', 'z' => 'w' ) ). Here's what
the input() method
currently looks like:
public static function input( $name, $value = null, $type =
'text', $attribs = array() ) {
There are three "special" attributes: name, value, and type. In the
Xml version, the special attributes are instead name, size, and value.
The problem with this is that 1) none of these three are actually
required, and at least two of the three frequently just use the
default; and 2) the order isn't obvious or easy to remember.
Moreover, in practice, the $attribs are usually specified, so the
parameter defaults don't help much. If we have "convenience"
shortcuts like this for many other elements, they'll indeed be more
concise, but hard to actually understand or use.
So we could do this . . .
public static function input( $attribs = array() ) {
. . . but that's not really convenient at all.
So one way that Xml handles this is to just have even more specialized
functions, like:
public static function hidden( $name, $value, $attribs = array() ) {
The nice thing here is that there's practically no reason you'd ever
want to make a hidden input without both name and value specified, but
often those are the only things you want to specify. So often you can
replace Html::element( 'input', array( 'type' => 'hidden',
'name' =>
'myName', 'value' => 'foo' ) ) with just Html::hidden(
'myName', 'foo'
), and that's pretty clear, since hidden inputs logically have two
things they almost always need to have specified, and there's an
obvious order (key then value).
So I'm thinking that the general tactic should be 1) have special
parameters for anything that's required or practically always used,
but 2) relegate everything else to the $attribs array. So if we had a
hypothetical Html::a(), it would look like:
public static function a( $href, $attribs = array() ) {
and img might be:
public static function img( $src, $alt, $attribs = array() ) {
On the other hand, this would mean you'd have to do something like
public static function input( $name, $attribs = array() ) {
which is fairly verbose in practice. Of course, as noted, you
normally need to use $attribs anyway for input, so it's not really
that much worse than necessary.
What are other people's thoughts on what a nice interface would look
like here? (Other than "PHP should have named parameters like
Python"? :) ) Xml is very inconsistent on this score, and it would
be nice if we had a consistent format for Html.
Show replies by date