天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

PHP 組件化編程技巧

但其在UI方便卻有些力不從心,不僅是php,任何一種Web編程語言在設(shè)計(jì)UI都有類似的問題,宿主語言與HTML混和在一個(gè)文件中,大量重復(fù)的 HTML代碼,毫無任何技術(shù)含量,但又非常的費(fèi)時(shí)費(fèi)力。于是我就希望能夠?qū)χ白鲞^的php項(xiàng)目UI部分進(jìn)行總結(jié)和歸納,將其封裝為一個(gè)個(gè)小的組件(就像 Delphi中的組件一樣),在界面上呈現(xiàn)為統(tǒng)一的風(fēng)格,日后可以再針對這結(jié)組件編寫多個(gè)CSS文件,提供“換膚”功能。

所有的組件都繼承自AbatractComponent這個(gè)類,并實(shí)現(xiàn)其中的toString()render()方法。AbatractComponent又有三個(gè)主要的子類,一個(gè)是容器類Continer,其又派生出PanelPopPanelGroupPanel等類,第二個(gè)是控件類Control,是所有可視控件類的父類,如ButtonLinkButton等類,第三個(gè)則是列表類List,實(shí)現(xiàn)有列表,名-值對的UI。

繼承圖

AbstractComponent部分代碼:
復(fù)制代碼 代碼如下:
<?php
/**
* Component Library
*
* @author Chris Mao
* @package Component
* @description All components must be extened from the class
* and override the both methods of toString.
* @copyright Copyright (c) 2009 JueRui Soft Studio
*
**/
class AbstractComponent {

/*
* @var _style the component style's array
*
* @access protected
*
*/
protected $_style = array();
/*
* @var _attributes the component attribute's string
*
* @access protected
*
*/
protected $_attributes = array();

/**
* constructor function
*
* @access public
*
*/
public function __construct($options = null, $style = null) {
if (!is_null($options) && (gettype($options) != "array")) {
throw new Exception("The options must be a array!!");
}
if (!empty($options) && is_array($options)) {
if (array_key_exists("style", $options)) {
if (is_array($options["style"])) {
$this->_style = array_merge($this->_style, $options["style"]);
}
unset($options["style"]);
}
$this->_attributes = array_merge($this->_attributes, $options);
}
if (!empty($style) && is_array($style)) {
$this->_style = array_merge($this->_style, $style);
}
}

/**
* set the component attributes
*
* @access protected
*
* @param $name attribute name
* @param $value attribute value, option
*
* @return AbstractComponent
*/
protected function setAttr($name, $value) {
if (array_key_exists($name, $this->_attributes)) {
unset($this->_attributes[$name]);
}
$this->_attributes[$name] = $value;
return $this;
}

/**
* get the component attributes' value
*
* @access protected
*
* @param $name attribute name
*
* @return string
*/
protected function getAttr($name) {
return array_key_exists($name, $this->_attributes) ? $this->_attributes[$name] : null;
}

/**
* set the component style
*
* @access protected
*
* @param $name style name
* @param $value style value, option
*
* @return AbstractComponent
*/
protected function setStyle($name, $value) {
if (array_key_exists($name, $this->_style)) {
unset($this->_style[$name]);
}
$this->_style[$name] = $value;
return $this;
}

/**
* get the component style's value
*
* @access protected
*
* @param $name attribute name
*
* @return string
*/
protected function getStyle($name) {
return array_key_exists($name, $this->_style) ? $this->_style[$name] : null;
}

/**
* convert the component all attributes to string like name = "value"
*
* @access protected
*
* @return string
*/
protected function attributeToString() {
//$s = array_reduce(;
$s = "";
foreach($this->_attributes as $key => $value) {
$s .= " $key=/"$value/" ";
}
return $s;
}

/**
* convert the component style to string like style = "....."
*
* @access protected
*
* @return string
*/
protected function styleToString() {
if (empty($this->_style)) return "";
$s = "";
foreach($this->_style as $key => $value) {
$s .= " $key: $value; ";
}
$s = " style=/"$s/" ";
return $s;
}

/**
* set or get the component attributes
*
* @access public
*
* @param $name attribute name
* @param $value attribute value, option
*
* @return string || AbstractComponent
*/
public function attr() {
$name = func_get_arg(0);
if (func_num_args() == 1) {
return $this->getAttr($name);
}
else if (func_num_args() == 2) {
$value = func_get_arg(1);
return $this->setAttr($name, $value);
}
}

/**
* set or get the component style
*
* @access public
*
* @param $name style name
* @param $value style value, option
*
* @return string || AbstractComponent
*/
public function style() {
$name = func_get_arg(0);
if (func_num_args() == 1) {
return $this->getStyle($name);
}
else if (func_num_args() == 2) {
$value = func_get_arg(1);
return $this->setStyle($name, $value);
}
}

/**
* return the HTML string
*
* @access public
*
* @return string
**/
public function toString() {
thorw New AbstractException("subclass must be override this method!!");
}

/**
* render the component
*
* @access public
*
* @return void
**/
public function render() {
echo $this->toString();
}
}

php技術(shù)PHP 組件化編程技巧,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 私密按摩师在线观看 百度网盘 | 国色精品VA在线观看免费视频 | 97免费视频在线观看 | 色AV色婷婷66人妻久久久 | 97无码欧美熟妇人妻蜜 | 久久这里只有精品国产精品99 | 亚洲熟女片嫩草影院 | 久久精品国产首叶 | 99精品视频免费在线观看 | 日韩欧美1区| 无码人妻精品一区二区蜜桃在线看 | 香蕉久久一区二区三区啪啪 | 中文字幕在线免费观看视频 | 真实伦 乱 | 亚洲高清无在码在线电影 | 亚洲热在线视频 | 麻豆人妻无码性色AV | 国模玲玲自拍337p | 日本十八禁无遮拦啪啪漫画 | 久久秋霞理伦片 | 国产精品色吧国产精品 | 狠狠狠狠狠狠干 | 成人免费视频在线 | 精品久久久噜噜噜久久7 | 美女穿丝袜被狂躁动态图 | 双性诱受灌满哭求饶BL | 国产1广场舞丰满老女偷 | 国内精品国内自产视频 | 蜜臀AV久久国产午夜福利软件 | 伊人久久99热这里只有精品 | 亚洲 在线 日韩 欧美 | 美女尿口羞羞视频 | 久久草这里全是精品香蕉频线观 | 在线二区 中文 无码 | 亚州AV人片一区二区三区99久 | 亚洲日韩精品AV中文字幕 | 色戒无删减流畅完整版 | 老师好爽你下面水好多视频 | 欧美亚洲日韩自拍高清中文 | 免费看亚洲 | 一一本之道高清手机在线观看 |