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统计目录下的文件总数及代码行数(去除注释及空行)
Jan 17 PHP
php中用于检测一个地理IP地址是否可用的代码
Feb 19 PHP
php全局变量和类配合使用深刻理解
Jun 05 PHP
php加密解密实用类分享
Jan 07 PHP
PHP中鲜为人知的10个函数
Feb 28 PHP
php数组键名技巧小结
Feb 17 PHP
php修改上传图片尺寸的方法
Apr 14 PHP
学习php设计模式 php实现状态模式
Dec 07 PHP
thinkPHP导出csv文件及用表格输出excel的方法
Dec 30 PHP
Yii2框架数据库简单的增删改查语法小结
Aug 31 PHP
PHP的RSA加密解密方法以及开发接口使用
Feb 11 PHP
浅谈laravel框架sql中groupBy之后排序的问题
Oct 17 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
UCenter中的一个可逆加密函数authcode函数代码
2010/07/20 PHP
php获取数据库结果集方法(推荐)
2017/06/01 PHP
PHP多维数组排序array详解
2017/11/21 PHP
动态修改DOM 里面的 id 属性的弊端分析
2008/09/03 Javascript
JavaScript让IE浏览器event对象符合W3C DOM标准
2009/11/24 Javascript
jQuery对象数据缓存Cache原理及jQuery.data方法区别介绍
2013/04/07 Javascript
javascript制作坦克大战全纪录(1)
2014/11/27 Javascript
jquery简单实现外部链接用新窗口打开的方法
2015/05/30 Javascript
jQuery实现带延迟的二级tab切换下拉列表效果
2015/09/01 Javascript
使用jquery插件qrcode生成二维码
2015/10/22 Javascript
jQuery简单自定义图片轮播插件及用法示例
2016/11/21 Javascript
Vuejs 页面的区域化与组件封装的实现
2017/09/11 Javascript
利用pm2部署多个node.js项目的配置教程
2017/10/22 Javascript
LayerClose弹窗关闭刷新方法
2018/08/17 Javascript
原生JavaScript实现滑动拖动验证的示例代码
2019/12/06 Javascript
vue全屏事件开发详解
2020/06/17 Javascript
JS画布动态实现黑客帝国背景效果
2020/11/08 Javascript
[01:20]DOTA2 齐天大圣至宝动态展示
2016/12/13 DOTA
用Python将动态GIF图片倒放播放的方法
2016/11/02 Python
Python set常用操作函数集锦
2017/11/15 Python
python中(str,list,tuple)基础知识汇总
2018/02/20 Python
使用pandas将numpy中的数组数据保存到csv文件的方法
2018/06/14 Python
python实现人工智能Ai抠图功能
2019/09/05 Python
python处理excel绘制雷达图
2019/10/18 Python
python GUI库图形界面开发之PyQt5 Qt Designer工具(Qt设计师)详细使用方法及Designer ui文件转py文件方法
2020/02/26 Python
pandas读取csv文件提示不存在的解决方法及原因分析
2020/04/21 Python
HTML5 LocalStorage 本地存储刷新值还在
2017/03/10 HTML / CSS
Agoda台湾官网:国内外订房2折起
2018/03/20 全球购物
Elemental Herbology官网:英国美容品牌
2019/04/27 全球购物
个人自我鉴定范文
2013/10/04 职场文书
会计助理的岗位职责
2013/11/29 职场文书
租房协议书范本
2014/04/09 职场文书
一份文言文检讨书
2014/09/13 职场文书
2014个人年终工作总结范文
2014/12/15 职场文书
泰山导游词
2015/02/02 职场文书
难以忽视的真相观后感
2015/06/05 职场文书