Sådan benyttes komponenten Option klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Option.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Option::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Option($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Option klassen
Den fulde PHP kildekode for Option klassen
<?php/** * @package form * @filesource * @see HTML_FORM_COMPONENT_PATH.'/Option.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */require_once(HTML_BASE_UTIL_PATH.'/Element.php');require_once(HTML_BASE_UTIL_PATH.'/Htmlspecialchars.php');/** * Generates an OPTION form element * <code> * Usage: * $option = new Option($text, $value, $selected, $title, $label); * print $option->getHtml(); * Or * Option::display($text, $value, $selected, $title, $label); * </code> * @package form */class Option extends Element { /** * @var String $text The text to show to the user */ protected $text = ''; /** * @var String $selected The 'selected' text or '' */ protected $selected = ''; /** * @var String $label The label to show to the user */ protected $label = ''; /** * Constructor * @param String $text The text to show * @param String $value The value, if any * @param String $selected The option is selected * @param String $title The tooltip * @param String $label The label */ function __construct($text, $value='', $selected='', $title='', $label='') { $name = ''; // Not supported $class = ''; $tabindex = ''; $onclick = ''; $accesskey = ''; parent::__construct($name, $value, $class, $title, $tabindex, $onclick, $accesskey); $this->text = $text; $this->selected = $selected; if ($this->selected != '' && $this->selected != 'selected') { die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n".'Unknown option attribute found for selected='.$this->selected.' accepted values are "" or "selected"'); } $this->label = $label; // != ''?$label:$text } /** * Returns the html for the option control * @return String the complete html */ function getHtml() { $html = $this->html; $html .= "\t<option"; $html .= $this->getAttribute('value'); $html .= $this->getAttribute('selected'); $html .= $this->getAttribute('title'); $html .= $this->getAttribute('label'); $html .= '>'; $html .= Htmlspecialchars::encode($this->text); // Translate html special chars $html .= "</option>\r\n"; return $html; } /** * Display html * <code> * Usage: * Option::display($text, $value, $selected, $title, $label); * </code> * @param String $text The text to show * @param String $value The value, if any * @param String $selected The option is selected * @param String $title The tooltip * @param String $label The label */ public static function display($text, $value='', $selected='', $title='', $label='') { $html = new Option($text, $value, $selected, $title, $label); $html->addHtml(); }}?>
Den fulde HTML kildekode for Option klassen
<? <!-- DEBUG: Option --> <option value="Test">Test</option> ?>
Her er 'klasse metoderne' for Option klassen:
Her er 'objekt variable' for Option klassen: