程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP 組件化編程技巧

PHP 組件化編程技巧

編輯:關於PHP編程

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

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

繼承圖

AbstractComponent部分代碼:
復制代碼 代碼如下:
<?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();
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved