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 相关文章推荐
NOT NULL 和NULL
Jan 15 PHP
php Mysql日期和时间函数集合
Nov 16 PHP
利用ThinkPHP内置的ThinkAjax实现异步传输技术的实现方法
Dec 19 PHP
php函数array_merge用法一例(合并同类数组)
Feb 03 PHP
使用CodeIgniter的类库做图片上传
Jun 12 PHP
php smarty truncate UTF8乱码问题解决办法
Jun 13 PHP
php绘图之加载外部图片的方法
Jan 24 PHP
24条货真价实的PHP代码优化技巧
Jul 28 PHP
总结PHP中数值计算的注意事项
Aug 14 PHP
php进行md5加密简单实例方法
Sep 19 PHP
PHP copy函数使用案例代码解析
Sep 01 PHP
PHP7 新增常量
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
php入门小知识
2008/03/24 PHP
PHP延迟静态绑定示例分享
2014/06/22 PHP
php返回字符串中所有单词的方法
2015/03/09 PHP
ThinkPHP使用Smarty第三方插件方法小结
2016/03/19 PHP
直接生成打开窗口代码,不必下载
2008/05/14 Javascript
javascript 面向对象全新理练之数据的封装
2009/12/03 Javascript
详谈JavaScript内存泄漏
2014/11/14 Javascript
node.js实现BigPipe详解
2014/12/05 Javascript
z-blog SyntaxHighlighter 长代码无法换行解决办法(基于jquery)
2015/11/18 Javascript
分享jQuery封装好的一些常用操作
2016/07/28 Javascript
jQuery dataTables与jQuery UI 对话框dialog的使用教程
2016/09/02 Javascript
jQuery Ajax实现跨域请求
2017/01/21 Javascript
JavaScript数据结构之数组的表示方法示例
2017/04/12 Javascript
JS获取短信验证码倒计时的实现代码
2017/05/22 Javascript
Node.js使用Express.Router的方法
2017/11/14 Javascript
layer实现关闭弹出层刷新父界面功能详解
2017/11/15 Javascript
nodejs爬虫初试superagent和cheerio
2018/03/05 NodeJs
jQuery实现获取多选框的值示例
2020/02/07 jQuery
最全vue的vue-amap使用高德地图插件画多边形范围的示例代码
2020/07/17 Javascript
浅谈Ant Design Pro 菜单自定义 icon
2020/11/17 Javascript
[57:37]EG vs Mineski 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
[35:55]完美世界DOTA2联赛PWL S3 Rebirth vs CPG 第一场 12.11
2020/12/13 DOTA
Python实现的生成自我描述脚本分享(很有意思的程序)
2014/07/18 Python
python进阶教程之异常处理
2014/08/30 Python
在Python的gevent框架下执行异步的Solr查询的教程
2015/04/16 Python
python画出三角形外接圆和内切圆的方法
2018/01/25 Python
python 获得任意路径下的文件及其根目录的方法
2019/02/16 Python
pandas DataFrame 行列索引及值的获取的方法
2019/07/02 Python
纯CSS3发光分享按钮的实现教程
2014/09/06 HTML / CSS
台湾最大银发乐活百货:乐龄网
2018/05/21 全球购物
美国办公用品折扣网站:Shoplet.com
2019/11/24 全球购物
SNIDEL官网:日本VIVI杂志人气少女第一品牌
2020/03/12 全球购物
大专毕业自我鉴定
2014/02/04 职场文书
房屋财产继承协议书范本
2014/11/03 职场文书
上学路上观后感
2015/06/16 职场文书
WIN10使用IIS部署ftp服务器详细教程
2022/08/05 Servers