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实现的简单压缩英文字符串的代码
Apr 24 PHP
php+AJAX传送中文会导致乱码的问题的解决方法
Sep 08 PHP
PHP 年龄计算函数(精确到天)
Jun 07 PHP
探讨php中遍历二维数组的几种方法详解
Jun 08 PHP
php导出excel格式数据问题
Mar 11 PHP
用PHP来计算某个目录大小的方法
Apr 01 PHP
PHP判断远程图片是否存在的几种方法
May 04 PHP
PHP+JavaScript实现无刷新上传图片
Feb 21 PHP
Thinkphp结合ajaxFileUpload实现异步图片传输示例
Mar 13 PHP
php实现评论回复删除功能
May 23 PHP
[原创]PHP global全局变量经典应用与注意事项分析【附$GLOBALS用法对比】
Jul 12 PHP
PHP7移除的扩展和SAPI
Mar 09 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
apache+mysql+php+ssl服务器之完全安装攻略
2006/09/05 PHP
php使用Jpgraph绘制3D饼状图的方法
2015/06/10 PHP
jQuery bxCarousel实现图片滚动切换效果示例代码
2013/05/15 Javascript
jQuery学习笔记之jQuery动画效果
2013/09/09 Javascript
JS使用eval解析JSON的注意事项分析
2015/11/14 Javascript
jQuery实现导航样式布局操作示例【可自定义样式布局】
2018/07/24 jQuery
改进 JavaScript 和 Rust 的互操作性并深入认识 wasm-bindgen 组件
2019/07/13 Javascript
小程序实现锚点滑动效果
2019/09/23 Javascript
使用layer模态框给新页面传值的方法
2019/09/27 Javascript
selenium 反爬虫之跳过淘宝滑块验证功能的实现代码
2020/08/27 Javascript
vue 路由meta 设置导航隐藏与显示功能的示例代码
2020/09/04 Javascript
基于python的汉字转GBK码实现代码
2012/02/19 Python
python从ftp下载数据保存实例
2013/11/20 Python
python实现可以断点续传和并发的ftp程序
2016/09/13 Python
Python编程实现输入某年某月某日计算出这一天是该年第几天的方法
2017/04/18 Python
Python解析Excle文件中的数据方法
2018/10/23 Python
python用插值法绘制平滑曲线
2021/02/19 Python
Django1.11自带分页器paginator的使用方法
2019/10/31 Python
在python shell中运行python文件的实现
2019/12/21 Python
python GUI库图形界面开发之PyQt5复选框控件QCheckBox详细使用方法与实例
2020/02/28 Python
pytorch简介
2020/11/11 Python
Javascript 高级手势使用介绍
2013/04/21 HTML / CSS
荷兰手表网站:Watch2Day
2018/07/02 全球购物
Hotels.com英国:全球领先的酒店住宿提供商
2019/01/24 全球购物
娇韵诗香港官网:Clarins香港
2020/08/13 全球购物
请描述一下”is a”关系和”has a”关系
2015/02/03 面试题
长青弘远的面试题
2012/06/09 面试题
法学研究生自我鉴定范文
2013/12/04 职场文书
绘画专业自荐信范文
2014/02/23 职场文书
烹饪自我鉴定
2014/03/01 职场文书
贷款委托书范本
2014/04/08 职场文书
计算机专业自荐信
2014/05/24 职场文书
建筑横幅标语
2014/10/09 职场文书
关于成立领导小组的通知
2015/04/23 职场文书
慰问信(范文3篇)
2019/10/23 职场文书
前端与RabbitMQ实时消息推送未读消息小红点实现示例
2022/07/23 Java/Android