首頁  >  文章  >  php教程  >  PHP 組件化程式設計技巧

PHP 組件化程式設計技巧

黄舟
黄舟原創
2016-12-14 13:09:281062瀏覽

但其在UI方便卻有些力不從心,不僅是PHP,任何一種Web程式語言在設計UI都有類似的問題,宿主語言與HTML混和在一個文件中,大量重複的HTML代碼,毫無任何技術含量,但又非常的費時費力。於是我就希望能夠對之前做過的PHP項目UI部分進行總結和歸納,將其封裝為一個個小的組件(就像Delphi中的組件一樣),在界面上呈現為統一的風格,日後可以再針對這結組件編寫多個CSS文件,提供「換膚」功能。

所有的元件都繼承自AbatractComponent這個類,並實作其中的toString()和render()方法。 AbatractComponent又有三個主要的子類,一個是容器類Continer,其又派生出Panel、PopPanel和GroupPanel等類,第二個是控件類Control,是所有可視控件類的父類,如Button、LinkBut​​ton等類,第三個則是列表類List,實作有列表,名稱-值對的UI。

 

AbstractComponent 部分代碼:
/** 
* 元件庫 

* @author Chris Mao 
* @package 元件
* @description 所有元件必須從類別
* 擴充並重寫 toString 的兩個方法。 
* @copyright 版權所有 (c) 2009 爵銳軟體工作室

**/ 
class AbstractComponent { 

/* 
* @var _ _style = 陣列(); 
/* 
* @var _attributes 元件屬性的字串 

* @access protected 

*/ 
protected $_attributbutn(attri); public function __construct($options = null, $style = null) { 
if (!is_null($options) && (gettype($options) != "array")) {
拋出new Exception("選項必須是數組! )) { 
$ this->_style = array_merge($this->_style, $options["style"]); 

unset($options["style"]); 

unset($options["style"]); 

unset($options["style"]); 
} array_merge($this->_attributes, $options); 

if (!empty($style) && is_array($style)) {
$this->_style = array_merge($this->_style, $style);



/** 
* 建構子 

* @access public 

*/ 
受保護函數setAttr($name, $value) { 
if (array_key_exists($name, $this->_attributes)) {S ) ;_屬性[$名稱]); 

$this->_attributes[$name] = $value; 
回傳$this; 


/** 
* 設定元件屬性 

* @access protected 

* @param $name 屬性名稱 
* @param $value 屬性值,選項

*Compreturn Abstract
**/ 
受保護函數getAttr($ name) {
return array_key_exists($name, $this->_attributes) ? $this->_attributes[$name] : null; 


/**set 
* 取得元件屬性值 

* @access protected 

* @param $name 屬性名稱 

* @return string 
*/ value) { 
if (array_key_exists($name, $this->_style)) { 
unset($this->_style) [$名稱]); 

$this->_style[$name] = $value;
回傳$this; 


/** 
* 設定元件樣式

* @access protected

* @param $name 樣式名稱
* @param $value 樣式值、選項

* @return Abstracton*/ 
受保護函數getStyle($name) { 
return array_key_exists($name, $this->_style) ?this $this->_style[$name] : null; 


/** 
* 取得元件樣式值

* @access protected

* @param $name 屬性名稱

* @return string
*/ 


/** 
* 將元件所有屬性轉換為字串,如 name = "value" 

* @access protected 

* @return string 
*/ 
受保護的函數attributeToString() { 
//$s = array_reduce(; 
$s = ""; 
for]$ key =>; $value) { 
$s .= " $key="$value" "; 

return $s; 

/** 
* 將元件樣式轉換為字串,如 style = "....." 

* @access protected 

* @return string 
*/ 
受保護函數() { 
/** 
* 設定或取得元件屬性

* @access public

* @param $name 屬性名稱
* @param $value 屬性值,選項
**/ 
受保護函數() { (empty( $this->_style)) return "";
$s = "";
foreach($this->_style as $key => $value) { 
$s .= " $key: $value; "; 

$s = " style="$s" "; 
return $s; 


/**&*/ 
public function attr() {getſ
/**&*/ 
public function attr() {getſ
($unc);
if (func_num_args() == 1) { 
return $this->getAttr($name); 

else if (func_num_args() == 2) { 
$value = func_get_arg(1); >setAttr($name, $value); 



/** 
* 設定或取得元件樣式

* @access public

* @param $name 樣式名稱
* @param $value 樣式值,選項

* @return string ||*/ 
public function style() { 
$name = func_get_arg(0); 
if ( { 
return $this->getStyle($name); 

else if (func_num_args() == 2) { 
$value = func_get_arg(1); 
return $this-name,Svalue = func_get_arg(1); 
return $this-name. ; 



/** 
* 回傳 HTML 字串 

* @access public 

* @return string 
**/ 
public function toString() { 
thorw New AbstractException("子類別必須重寫此方法!!");#
} */ 
public function render() { 
echo $this->toString(); 

}

更多相關內容請關注PHP中文網(www.php.cn)!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn