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三层结构(上) 简单三层结构
Jul 04 PHP
php入门学习知识点一 PHP与MYSql连接与查询
Jul 14 PHP
用来解析.htpasswd文件的PHP类
Sep 05 PHP
php多用户读写文件冲突的解决办法
Nov 06 PHP
使用PHP生成二维码的两种方法(带logo图像)
Mar 14 PHP
php冒泡排序与快速排序实例详解
Dec 07 PHP
PHP实现伪静态方法汇总
Jan 13 PHP
thinkphp表单上传文件并将文件路径保存到数据库中
Jul 28 PHP
Laravel使用scout集成elasticsearch做全文搜索的实现方法
Nov 30 PHP
PHP 模拟登陆功能实例详解
Sep 10 PHP
详解no input file specified 三种解决方法
Nov 29 PHP
php使用redis的有序集合zset实现延迟队列应用示例
Feb 20 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判断输入是否是纯数字,英文,汉字的方法
2015/03/05 PHP
PHP获取音频文件的相关信息
2015/06/22 PHP
详谈phpAdmin修改密码后拒绝访问的问题
2017/04/03 PHP
tp5(thinkPHP5框架)时间查询操作实例分析
2019/05/29 PHP
JS 对象介绍
2010/01/20 Javascript
ExtJs使用IFrame的实现代码
2010/03/24 Javascript
javascript学习笔记(三) String 字符串类型介绍
2012/06/19 Javascript
jquery动态添加删除div 具体实现
2013/07/20 Javascript
jQuery实现contains方法不区分大小写的方法
2015/02/13 Javascript
jQuery团购倒计时特效实现方法
2015/05/07 Javascript
Angularjs实现多个页面共享数据的方式
2016/03/29 Javascript
AngularJS ng-mousedown 指令
2016/08/02 Javascript
关于javascript中限定时间内防止按钮重复点击的思路详解
2016/08/16 Javascript
Node.js connect ECONNREFUSED错误解决办法
2016/09/15 Javascript
JavaScript 对象详细整理总结
2016/09/29 Javascript
jQuery实现div跟随鼠标移动
2020/08/20 jQuery
使用JavaScript进行表单校验功能
2017/08/01 Javascript
vue2.0开发入门笔记之.vue文件的生成和使用
2017/09/19 Javascript
微信小程序媒体组件详解(视频,音乐,图片)
2017/09/19 Javascript
浅谈Koa2框架利用CORS完成跨域ajax请求
2018/03/06 Javascript
微信小程序实现限制用户转发功能的实例代码
2020/02/22 Javascript
如何实现js拖拽效果及原理解析
2020/05/08 Javascript
Python代码的打包与发布详解
2014/07/30 Python
python使用正则表达式分析网页中的图片并进行替换的方法
2015/03/26 Python
Python的净值数据接口调用示例分享
2016/03/15 Python
Python序列循环移位的3种方法推荐
2018/04/09 Python
Selenium(Python web测试工具)基本用法详解
2018/08/10 Python
python实现读取excel文件中所有sheet操作示例
2019/08/09 Python
python scipy卷积运算的实现方法
2019/09/16 Python
python3实现从kafka获取数据,并解析为json格式,写入到mysql中
2019/12/23 Python
Django media static外部访问Django中的图片设置教程
2020/04/07 Python
大学生就业自我鉴定
2013/10/26 职场文书
酒店总经理助理职责
2014/02/12 职场文书
颐和园的导游词
2015/01/30 职场文书
周恩来的四个昼夜观后感
2015/06/03 职场文书
MySQL8.0.24版本Release Note的一些改进点
2021/04/22 MySQL