Installation:
composer require pear/html_common2
Note: Requires PHP 5.3+ (conflicts with Laravel 8+). Use only in legacy projects or PHP 5.6+ environments.
Basic Usage:
require_once 'HTML/Common2.php';
$html = new HTML_Common2();
$html->setAttribute('class', 'btn btn-primary');
$html->setAttribute('data-toggle', 'modal');
echo $html->parseAttributes(); // Output: class="btn btn-primary" data-toggle="modal"
First Use Case:
// In a Laravel service
public function generateButtonAttributes($isPrimary = true) {
$html = new HTML_Common2();
$html->setAttribute('class', 'btn' . ($isPrimary ? ' btn-primary' : ''));
$html->setAttribute('role', 'button');
return $html->parseAttributes();
}
Where to Look First:
tests/ directory in the package for usage examples.setAttribute(), parseAttributes(), and mergeAttributes() for core functionality.Attribute Management:
$html = new HTML_Common2();
$html->setAttribute('id', 'user-profile');
$html->setAttribute('class', 'profile-card');
$html->mergeAttributes(['data-user-id' => 123, 'aria-hidden' => 'true']);
$html->removeAttribute('aria-hidden');
CSS Class Handling:
$html->addClass('active');
$html->removeClass('disabled');
if ($isActive) {
$html->addClass('active');
}
Document-Wide Options:
$html->setDocumentOption('charset', 'UTF-8');
$html->setDocumentOption('indent', ' ');
$html->setDocumentOption('linebreak', "\n");
$html->setDocumentOption('indent', ' ');
$html->setDocumentOption('linebreak', "\n");
echo $html->parseAttributes(); // Output with indentation
HTML Comments:
$html->addComment('This is a button');
echo $html->parseAttributes(); // Output: <!-- This is a button -->
Form Builder Integration:
HTML_Common2 as a base for custom form field generators.class FormField {
protected $html;
public function __construct() {
$this->html = new HTML_Common2();
}
public function setType($type) {
$this->html->setAttribute('type', $type);
}
public function getAttributes() {
return $this->html->parseAttributes();
}
}
Dynamic Template Rendering:
// app/Helpers/html_helper.php
if (!function_exists('html_attributes')) {
function html_attributes(array $attributes) {
$html = new HTML_Common2();
$html->mergeAttributes($attributes);
return $html->parseAttributes();
}
}
<button {{ html_attributes(['class' => 'btn btn-success', 'data-id' => $user->id]) }}>
Save
</button>
Legacy PEAR Migration:
HTML_QuickForm2) with HTML_Common2 for attribute handling.// Old PEAR way
$form->addElement('text', 'username', ['class' => 'form-control']);
// New way with HTML_Common2
$html = new HTML_Common2();
$html->setAttribute('class', 'form-control');
$form->addElement('text', 'username', $html->parseAttributes());
Laravel Service Provider:
HTML_Common2 to the Laravel container for dependency injection.// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton('html.common', function () {
return new \HTML_Common2();
});
}
public function __construct(\HTML_Common2 $htmlCommon) {
$this->htmlCommon = $htmlCommon;
}
Caching Attributes:
public function getCachedAttributes($key, $attributes) {
return Cache::remember("html.{$key}", 60, function () use ($attributes) {
$html = new HTML_Common2();
$html->mergeAttributes($attributes);
return $html->parseAttributes();
});
}
Testing:
HTML_Common2 in PHPUnit tests.$mockHtml = $this->createMock(\HTML_Common2::class);
$mockHtml->method('parseAttributes')->willReturn('class="test"');
$this->app->instance(\HTML_Common2::class, $mockHtml);
PHP Version Conflict:
illuminate/html.PEAR Autoloading:
require_once 'HTML/Common2.php' may fail.$loader = require __DIR__.'/vendor/autoload.php';
$html = new \HTML_Common2();
No PSR Compliance:
Deprecated Methods:
No Modern PHP Features:
Global State:
HTML_Common2 for each independent HTML generation task.Attribute Parsing Issues:
parseAttributes() outputs unexpected results.$html = new HTML_Common2();
$html->setAttribute('class', 'btn btn-primary');
$html->setAttribute('data-toggle', 'modal');
var_dump($html->getAttributes()); // Inspect raw attributes
echo $html->parseAttributes(); // Inspect parsed output
Class Merging Problems:
class="btn btn-primary" becomes class="btn-primary").$html = new HTML_Common2();
$html->setAttribute('class', 'btn');
$html->addClass('btn-primary');
var_dump($html->getAttribute('class')); // Should output: 'btn btn-primary'
Indentation Issues:
$html = new HTML_Common2();
$html->setDocumentOption('indent', ' ');
$html->setDocumentOption('linebreak', "\n");
$html->setAttribute('div', ['class' => 'container']);
echo
How can I help you explore Laravel packages today?