PHP 组件化编程技巧


Posted in PHP onJune 06, 2009

但其在UI方便却有些力不从心,不仅是PHP,任何一种Web编程语言在设计UI都有类似的问题,宿主语言与HTML混和在一个文件中,大量重复的 HTML代码,毫无任何技术含量,但又非常的费时费力。于是我就希望能够对之前做过的PHP项目UI部分进行总结和归纳,将其封装为一个个小的组件(就像 Delphi中的组件一样),在界面上呈现为统一的风格,日后可以再针对这结组件编写多个CSS文件,提供“换肤”功能。

所有的组件都继承自AbatractComponent这个类,并实现其中的toString()render()方法。AbatractComponent又有三个主要的子类,一个是容器类Continer,其又派生出PanelPopPanelGroupPanel等类,第二个是控件类Control,是所有可视控件类的父类,如ButtonLinkButton等类,第三个则是列表类List,实现有列表,名-值对的UI。

PHP 组件化编程技巧

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(); 
} 
}
PHP 相关文章推荐
利用PHP和AJAX创建RSS聚合器的代码
Mar 13 PHP
php设计模式 DAO(数据访问对象模式)
Jun 26 PHP
解析如何修改phpmyadmin中的默认登陆超时时间
Jun 25 PHP
PHP小技巧之JS和CSS优化工具Minify的使用方法
May 19 PHP
Linux下PHP连接Oracle数据库
Aug 20 PHP
php之readdir函数用法实例
Nov 13 PHP
PHP生成唯一订单号的方法汇总
Apr 16 PHP
PHP date函数常用时间处理方法
May 11 PHP
php中header设置常见文件类型的content-type
Jun 23 PHP
PHP多进程编程实例详解
Jul 19 PHP
PHP在同一域名下两个不同的项目做独立登录机制详解
Sep 22 PHP
解决php用mysql方式连接数据库出现Deprecated报错问题
Dec 25 PHP
PHP加速 eAccelerator配置和使用指南
Jun 05 #PHP
php 更新数据库中断的解决方法
Jun 05 #PHP
php split汉字
Jun 05 #PHP
phpinfo 系统查看参数函数代码
Jun 05 #PHP
PHP 字符串 小常识
Jun 05 #PHP
PHP 批量删除 sql语句
Jun 05 #PHP
PHP 文件扩展名 获取函数
Jun 03 #PHP
You might like
php实现按文件名搜索文件的远程文件查找器
2014/05/10 PHP
smarty内置函数capture用法分析
2015/01/22 PHP
PHP面向对象程序设计类的定义与用法简单示例
2016/12/27 PHP
PHP实现链表的定义与反转功能示例
2018/06/09 PHP
php实现单笔转账到支付宝功能
2018/10/09 PHP
javascript 写类方式之十
2009/07/05 Javascript
jQuery-Easyui 1.2 实现多层菜单效果的代码
2012/01/13 Javascript
javaScript 动态访问JSon元素示例代码
2013/08/30 Javascript
jQuery实现拖动调整表格单元格大小的代码实例
2015/01/13 Javascript
详解js跨域原理以及2种解决方案
2015/12/09 Javascript
即将发布的jQuery 3 有哪些新特性
2016/04/14 Javascript
Vue中添加过渡效果的方法
2017/03/16 Javascript
React-intl 实现多语言的示例代码
2017/11/03 Javascript
JavaScript数组排序reverse()和sort()方法详解
2017/12/24 Javascript
js调用设备摄像头的方法
2018/07/19 Javascript
Javascript和jquery在selenium的使用过程
2019/10/31 jQuery
jquery实现有过渡效果的tab切换
2020/07/17 jQuery
python生成excel的实例代码
2017/11/08 Python
python多维数组切片方法
2018/04/13 Python
Python 循环语句之 while,for语句详解
2018/04/23 Python
Python中分支语句与循环语句实例详解
2018/09/13 Python
pyspark操作MongoDB的方法步骤
2019/01/04 Python
Python两台电脑实现TCP通信的方法示例
2019/05/06 Python
对Tensorflow中tensorboard日志的生成与显示详解
2020/02/04 Python
解决Django Haystack全文检索为空的问题
2020/05/19 Python
Python过滤序列元素的方法
2020/07/31 Python
css3类选择器之结合元素选择器和多类选择器用法
2017/03/09 HTML / CSS
英国鞋类及配饰零售商:Kurt Geiger
2017/02/04 全球购物
英国家庭珠宝商:T. H. Baker
2018/02/08 全球购物
BASIC HOUSE官方旗舰店:韩国著名的服装品牌
2018/09/27 全球购物
新大陆软件面试题
2016/11/24 面试题
房地产经营管理专业自荐信
2014/09/02 职场文书
2015年秋季校长开学典礼致辞
2015/07/29 职场文书
大学军训心得体会800字
2016/01/11 职场文书
eval(cmd)与eval($cmd)的区别与联系
2021/07/07 PHP
使用HttpSessionListener监听器实战
2022/03/17 Java/Android