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+javascript液晶时钟
Oct 09 PHP
php读取xml实例代码
Jan 28 PHP
在PHP中使用curl_init函数的说明
Nov 02 PHP
PHP 中检查或过滤IP地址的实现代码
Nov 27 PHP
解析CI即CodeIgniter框架在Nginx下的重写规则
Jun 03 PHP
PHP中模拟处理HTTP PUT请求的例子
Jul 22 PHP
PHP操作MySQL事务实例
Nov 05 PHP
制作安全性高的PHP网站的几个实用要点
Dec 30 PHP
php 遍历目录,生成目录下每个文件的md5值并写入到结果文件中
Dec 12 PHP
php 三大特点:封装,继承,多态
Feb 19 PHP
Laravel 5.4重新登录实现跳转到登录前页面的原理和方法
Jul 13 PHP
php swoole多进程/多线程用法示例【基于php7nts版】
Aug 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调用MySQL存储过程并返回值的方法
2014/12/26 PHP
PHP使用Redis实现Session共享的实现示例
2019/05/12 PHP
Laravel关系模型指定条件查询方法
2019/10/10 PHP
js表格分页实现代码
2009/09/18 Javascript
jQuery对表单元素的取值和赋值操作代码
2011/05/19 Javascript
在jquery中处理带有命名空间的XML数据
2011/06/13 Javascript
jquery制作select列表双向选择示例代码
2014/09/02 Javascript
Js和JQuery获取鼠标指针坐标的实现代码分享
2015/05/25 Javascript
javascript合并表格单元格实例代码
2016/01/03 Javascript
AngularJS中的$watch(),$digest()和$apply()区分
2016/04/04 Javascript
Bootstrap框架动态生成Web页面文章内目录的方法
2016/05/12 Javascript
Bootstrap模块dropdown实现下拉框响应
2016/05/22 Javascript
JS常见算法详解
2017/02/28 Javascript
手机端转换rem适应
2017/04/01 Javascript
JavaScript程序设计高级算法之动态规划实例分析
2017/11/24 Javascript
jQuery实现的简单对话框拖动功能示例
2018/06/05 jQuery
原生JS实现$.param() 函数的方法
2018/08/10 Javascript
vue-cli脚手架build目录下utils.js工具配置文件详解
2018/09/14 Javascript
VuePress 静态网站生成方法步骤
2019/02/14 Javascript
JavaScript实现图片上传并预览并提交ajax
2019/09/30 Javascript
vue 项目引入echarts 添加点击事件操作
2020/09/09 Javascript
nodejs+express最简易的连接数据库的方法
2020/12/23 NodeJs
Python写的贪吃蛇游戏例子
2014/06/16 Python
Python3实现发送QQ邮件功能(附件)
2020/12/23 Python
利用python如何处理nc数据详解
2018/05/23 Python
基于python 微信小程序之获取已存在模板消息列表
2019/08/05 Python
详解用Python为直方图绘制拟合曲线的两种方法
2019/08/21 Python
python mongo 向数据中的数组类型新增数据操作
2020/12/05 Python
CSS3 rgb and rgba(透明色)的使用详解
2020/09/25 HTML / CSS
求职简历中个人的自我评价
2013/12/25 职场文书
廉洁自律演讲稿
2014/05/22 职场文书
经营管理策划方案
2014/05/22 职场文书
教师聘用意向书
2015/05/11 职场文书
运动会报道稿大全
2015/07/23 职场文书
《作风建设永远在路上》心得体会
2016/01/21 职场文书
图解上海144收音机
2021/04/22 无线电