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 相关文章推荐
第九节--绑定
Nov 16 PHP
php统计文件大小,以GB、MB、KB、B输出
May 29 PHP
php根据分类合并数组的方法实例详解
Nov 06 PHP
Codeigniter+PHPExcel实现导出数据到Excel文件
Jun 12 PHP
php清空(删除)指定目录下的文件,不删除目录文件夹的实现代码
Sep 04 PHP
ThinkPHP 3.2 数据分页代码分享
Oct 14 PHP
php随机获取金山词霸每日一句的方法
Jul 09 PHP
PHP微信开发之模板消息回复
Jun 24 PHP
php socket通信简单实现
Nov 18 PHP
php连接微软MSSQL(sql server)完全攻略
Nov 27 PHP
php的laravel框架快速集成微信登录的方法
Dec 12 PHP
php 可变函数使用小结
Jun 12 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之第十天
2006/10/09 PHP
PHP+MYSQL会员系统的开发实例教程
2014/08/23 PHP
ZendFramework框架实现连接两个或多个数据库的方法
2016/12/08 PHP
非常不错的一个javascript 类
2006/11/07 Javascript
基于jQuery的history历史记录插件
2010/12/11 Javascript
innerHTML与jquery里的html()区别介绍
2012/10/12 Javascript
9行javascript代码获取QQ群成员具体实现
2013/10/16 Javascript
SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
2015/12/10 Javascript
Bootstrap树形控件使用方法详解
2016/01/27 Javascript
js+html5实现的自由落体运动效果代码
2016/01/28 Javascript
JS+CSS实现的漂亮渐变背景特效代码(6个渐变效果)
2016/03/25 Javascript
BootStrap3学习笔记(一)之网格系统
2016/05/20 Javascript
jQuery 3.0中存在问题及解决办法
2016/07/15 Javascript
微信小程序 轮播图swiper详解及实例(源码下载)
2017/01/11 Javascript
JQuery 进入页面默认给已赋值的复选框打钩
2017/03/23 jQuery
自定义vue全局组件use使用、vuex的使用详解
2017/06/14 Javascript
浅谈Angular4实现热加载开发旅程
2017/09/08 Javascript
AngularJS实现的简单拖拽功能示例
2018/01/02 Javascript
详解webpack loader和plugin编写
2018/10/12 Javascript
微信小程序 腾讯地图SDK 获取当前地址实现解析
2019/08/12 Javascript
Python简单实现enum功能的方法
2016/04/25 Python
Python设计模式之门面模式简单示例
2018/01/09 Python
详解python字节码
2018/02/07 Python
终端命令查看TensorFlow版本号及路径的方法
2018/06/13 Python
对Pandas MultiIndex(多重索引)详解
2018/11/16 Python
在Pandas中处理NaN值的方法
2019/06/25 Python
C#中有没有静态构造函数,如果有是做什么用的?
2016/06/04 面试题
广告学专业毕业生自荐信
2013/09/24 职场文书
应届生船舶驾驶求职信
2013/10/19 职场文书
员工培训心得体会
2013/12/30 职场文书
高中生的自我鉴定范文
2014/01/24 职场文书
大学竞选班干部演讲稿
2014/08/21 职场文书
2015年度招聘工作总结
2015/05/28 职场文书
大学宣传委员竞选稿
2015/11/19 职场文书
情况说明书格式及范文
2019/06/24 职场文书
5道关于python基础 while循环练习题
2021/11/27 Python