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 相关文章推荐
推荐Discuz!5的PHP代码高亮显示与实现可运行代码
Mar 15 PHP
PHP 开源框架22个简单简介
Aug 24 PHP
php Static关键字实用方法
Jun 04 PHP
实用PHP会员权限控制实现原理分析
May 29 PHP
win7下memCache的安装过程(具体操作步骤)
Jun 28 PHP
php封装的mongodb操作类代码
Aug 06 PHP
php中html_entity_decode实现HTML实体转义
Jun 13 PHP
Laravel框架实现抢红包功能示例
Oct 31 PHP
有关PHP 中 config.m4 的探索
Aug 26 PHP
PHPstorm激活码2020年5月13日亲测有效
Sep 17 PHP
PHP 99乘法表的几种实现代码
Oct 13 PHP
PHP 判断字符串是中文还是英文, 或者是中英混合
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
ThinkPHP视图查询详解
2014/06/30 PHP
javascript学习笔记(二) js一些基本概念
2012/06/18 Javascript
获取内联和链接中的样式(js代码)
2013/04/11 Javascript
用示例说明filter()与find()的用法以及children()与find()的区别分析
2013/04/26 Javascript
Jquery通过Ajax方式来提交Form表单的具体实现
2013/11/07 Javascript
CSS3,HTML5和jQuery搜索框集锦
2014/12/02 Javascript
Javascript变量的作用域和作用域链详解
2015/04/02 Javascript
关于JS中match() 和 exec() 返回值和属性的测试
2016/03/21 Javascript
JavaScript仿flash遮罩动画效果
2016/06/15 Javascript
jQuery实现三级联动效果
2017/03/02 Javascript
JS组件系列之MVVM组件构建自己的Vue组件
2017/04/28 Javascript
详解AngularJS1.6版本中ui-router路由中/#!/的解决方法
2017/05/22 Javascript
使用cookie绕过验证码登录的实现代码
2017/10/12 Javascript
不使用JavaScript实现菜单的打开和关闭效果demo
2018/05/01 Javascript
vue-lazyload使用总结(推荐)
2018/11/01 Javascript
在element-ui的select下拉框加上滚动加载
2019/04/18 Javascript
Laravel 如何在blade文件中使用Vue组件的示例代码
2020/06/28 Javascript
[07:48]DOTA2上海特级锦标赛主赛事首日RECAP
2016/03/04 DOTA
[01:56]生活中的妖精之七夕特别档
2016/08/09 DOTA
[10:24]郎朗助力完美“圣”典,天籁交织奏响序曲
2016/12/18 DOTA
Python操作Excel之xlsx文件
2017/03/24 Python
使用python编写udp协议的ping程序方法
2018/04/22 Python
Python3 读、写Excel文件的操作方法
2018/10/20 Python
Python字典的概念及常见应用实例详解
2019/10/30 Python
python代码如何实现余弦相似性计算
2020/02/09 Python
python 递归调用返回None的问题及解决方法
2020/03/16 Python
HTML5 canvas 瀑布流文字效果的示例代码
2018/01/31 HTML / CSS
俄罗斯三星品牌商店:Samsungstore
2020/04/05 全球购物
什么是Web Service?
2012/07/25 面试题
xxx同志考察材料
2014/02/07 职场文书
高一军训决心书
2015/02/05 职场文书
承诺保证书格式
2015/02/28 职场文书
个人年底工作总结
2015/03/10 职场文书
2015年办公室工作总结范文
2015/03/31 职场文书
Python中Matplotlib的点、线形状、颜色以及绘制散点图
2022/04/07 Python
tomcat下部署jenkins的方法
2022/05/06 Servers