但其在UI方便却有些力不从心,不仅是PHP,任何一种Web编程语言在设计UI都有类似的问题,宿主语言与HTML混和在一个文件中,大量重复的HTML代码,毫无任何技术含量,但又非常的费时费力。于是我就希望能够对之前做过的PHP项目UI部分进行总结和归纳,将其封装为一个个小的组件(就像Delphi中的组件一样),在界面上呈现为统一的风格,日后可以再针对这结组件编写多个CSS文件,提供“换肤”功能。
所有的组件都继承自AbatractComponent这个类,并实现其中的toString()和render()方法。AbatractComponent又有三个主要的子类,一个是容器类Continer,其又派生出Panel、PopPanel和GroupPanel等类,第二个是控件类Control,是所有可视控件类的父类,如Button、LinkButton等类,第三个则是列表类List,实现有列表,名-值对的UI。
AbstractComponent部分代码:
复制代码代码如下:
/**
*ComponentLibrary
*
*@authorChrisMao
*@packageComponent
*@descriptionAllcomponentsmustbeextenedfromtheclass
*andoverridethebothmethodsoftoString.
*@copyrightCopyright(c)2009JueRuiSoftStudio
*
**/
classAbstractComponent{
/*
*@var_stylethecomponentstyle'sarray
*
*@accessprotected
*
*/
protected$_array();
/*
*@var_attributesthecomponentattribute'sstring
*
*@accessprotected
*
*/
protected$_attributes=array();
/**
*constructorfunction
*
*@accesspublic
*
*/
publicfunction__construct($options=null,$null){
if(!is_null($options)&&(gettype($options)!="array")){
thrownewException("Theoptionsmustbeaarray!!");
}
if(!empty($options)&&is_array($options)){
if(array_key_exists("style",$options)){
if(is_array($options["style"])){
$this->_array_merge($this->_style,$options["style"]);
}
unset($options["style"]);
}
$this->_attributes=array_merge($this->_attributes,$options);
}
if(!empty($style)&&is_array($style)){
$this->_array_merge($this->_style,$style);
}
}
/**
*setthecomponentattributes
*
*@accessprotected
*
*@param$nameattributename
*@param$valueattributevalue,option
*
*@returnAbstractComponent
*/
protectedfunctionsetAttr($name,$value){
if(array_key_exists($name,$this->_attributes)){
unset($this->_attributes[$name]);
}
$this->_attributes[$name]=$value;
return$this;
}
/**
*getthecomponentattributes'value
*
*@accessprotected
*
*@param$nameattributename
*
*@returnstring
*/
protectedfunctiongetAttr($name){
returnarray_key_exists($name,$this->_attributes)?$this->_attributes[$name]:null;
}
/**
*setthecomponentstyle
*
*@accessprotected
*
*@param$namestylename
*@param$valuestylevalue,option
*
*@returnAbstractComponent
*/
protectedfunctionsetStyle($name,$value){
if(array_key_exists($name,$this->_style)){
unset($this->_style[$name]);
}
$this->_style[$name]=$value;
return$this;
}
/**
*getthecomponentstyle'svalue
*
*@accessprotected
*
*@param$nameattributename
*
*@returnstring
*/
protectedfunctiongetStyle($name){
returnarray_key_exists($name,$this->_style)?$this->_style[$name]:null;
}
/**
*convertthecomponentallattributestostringlikename="value"
*
*@accessprotected
*
*@returnstring
*/
protectedfunctionattributeToString(){
//$s=array_reduce(;
$s="";
foreach($this->_attributesas$key=>$value){
$s.="$key="$value"";
}
return$s;
}
/**
*convertthecomponentstyletostringlike....."
*
*@accessprotected
*
*@returnstring
*/
protectedfunctionstyleToString(){
if(empty($this->_style))return"";
$s="";
foreach($this->_styleas$key=>$value){
$s.="$key:$value;";
}
$s="$s"";
return$s;
}
/**
*setorgetthecomponentattributes
*
*@accesspublic
*
*@param$nameattributename
*@param$valueattributevalue,option
*
*@returnstring||AbstractComponent
*/
publicfunctionattr(){
$name=func_get_arg(0);
if(func_num_args()==1){
return$this->getAttr($name);
}
elseif(func_num_args()==2){
$value=func_get_arg(1);
return$this->setAttr($name,$value);
}
}
/**
*setorgetthecomponentstyle
*
*@accesspublic
*
*@param$namestylename
*@param$valuestylevalue,option
*
*@returnstring||AbstractComponent
*/
publicfunctionstyle(){
$name=func_get_arg(0);
if(func_num_args()==1){
return$this->getStyle($name);
}
elseif(func_num_args()==2){
$value=func_get_arg(1);
return$this->setStyle($name,$value);
}
}
/**
*returntheHTMLstring
*
*@accesspublic
*
*@returnstring
**/
publicfunctiontoString(){
thorwNewAbstractException("subclassmustbeoverridethismethod!!
");
}
/**
*renderthecomponent
*
*@accesspublic
*
*@returnvoid
**/
publicfunctionrender(){
echo$this->toString();
}
}
您可能感兴趣的文章:以实例全面讲解PHP中多进程编程的相关函数的使用PHP编程函数安全篇php高级编程-函数-郑阿奇PHP编程之高级技巧——利用Mysql函数PHP高级编程实例:编写守护进程php学习笔记之面向对象编程浅析PHP编程中10个最常见的错误谈谈新手如何学习PHP网络编程PHP编程实现多维数组按照某个键值排序的方法小结【2种方法】PHP编程风格规范分享PHP编程中的常见漏洞和代码实例php函数式编程简单示例