Attributes – Working with HTML attributes.

Methods for Attributes Handling

HTML_Common2 class is intended as a parent class for classes representing HTML elements and its main purpose is to allow easy attribute handling for instances of these child classes.

Inividual attribute values can be set by HTML_Common2::setAttribute() and read by HTML_Common2::getAttribute() methods. Use HTML_Common2::removeAttribute() to remove an attribute, calling setAttribute() without explicitly giving a new attribute value serves a different purpose:

// these calls are identical

You can completely replace attributes of HTML_Common2 instance by using HTML_Common2::setAttributes() method and add/replace several new attributes at once by using HTML_Common2::mergeAttributes(). By default constructor of HTML_Common2 calls mergeAttributes(), so that some default attributes can be provided by a subclass and only overridden if needed. Note that both of the above methods can accept either a string of HTML attributes or an array of these.

Finally, HTML_Common2::getAttributes() method returns the values of all the instance's attributes. Those can be returned either as an associative array or a string. As the package is intended for XHTML-compliant output, attribute names will always be lowercased and quotes will be used around attribute values when outputting the attribute string.

Using array notation

Since release 2.1.0 HTML_Common2 implements ArrayAccess interface, allowing access to attributes using array notation:

Accessing attributes as array keys

if (!isset($html['id'])) {
// equivalent to $html->setAttribute('id', 'foo');
$html['id'] = 'foo';
// equivalent to $html->setAttribute('checked');
$html[] = 'checked';
// equivalent to $html->removeAttribute('checked');

Working with CSS Classes

HTML_Common2 contains several methods to easily handle 'class' attribute of HTML tags: HTML_Common2::addClass(), HTML_Common2::removeClass() and HTML_Common2::hasClass(). Their behaviour should be easily deducable from their names:

->setAttribute('class''foo bar');
if (!
$html->hasClass('foo')) {

will output

bar notFoo

Usage Example

The following example shows a somewhat minimal subclass of HTML_Common2 and possible ways to change its attributes.

Methods available for attribute handling

// a non-abstract subclass of HTML_Common2 
class HTML_Tag_Foo extends HTML_Common2
// some predefined attributes, won't be overwritten
protected $attributes = array('class' => 'pretty');

// basic implementation of magic __toString() method
public function __toString()
'<foo' $this->getAttributes(true) . ' />';

$foo = new HTML_Tag_Foo(array('size' => 'small''align' => 'top left corner',
'foo' => 'foo value'));
// note how the attributes from this string will be handled
$foo->mergeAttributes("bar LEVEL=0 value='Whatever'");


The above code will output:

<foo class="pretty" size="smaller" foo="foo value" bar="bar" level="0" value="Whatever" />
HTML_Common2 (Previous) Setting document-wide options. (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.