PHP组件编程技巧
但是在UI上方便,但是不够。不仅仅是PHP,任何Web编程语言在UI设计上都有类似的问题。宿主语言和HTML混合在一个文件中,大量重复的HTML代码没有技术含量,但是非常费时费力。所以我希望对之前做过的PHP项目的UI部分进行总结归纳,打包成小组件(就像Delphi中的组件一样),在界面上以统一的风格呈现出来。以后我可以为这个组件编写多个CSS文件,提供“换肤”功能。的所有组件都从类AbatractComponent继承,并实现toString()和render()方法。AbatractComponent主要有三个子类,一个是Continer类,派生自Panel、PopPanel和GroupPanel,第二个是Control类,是所有可视化控件类的父类,比如Button和LinkButton,第三个是List类,用列表和名称-值对实现UI。
抽象组件部分代码:复制代码代码如下:php /** *组件库* * @作者克里斯毛* @包组件* @描述所有组件都必须从类*中进行扩展,并覆盖转换对象为字符串的两种方法。* @版权所有(c)2009 juerei Soft Studio * * */class抽象组件{/* * @ var _ style该组件的样式数组* * @访问受保护的* */受保护的$ _ style=array();/* * @var _attributes组件属性的字符串* * @访问受保护* */受保护$ _ attributes=array();/** *构造函数函数* * @ access public * */public function _ _ construct($ options=null,$style=null) { if(!is _ null($ options)(gettype($ options)!=' array '){ 0引发新异常('选项必须是数组!');} 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(!空($style)是_ array($ style)){ $ this-_ style=array _ merge($ this-_ style,$ style);} } /** *设置组件属性* * @访问受保护的* * @param $name属性名称* @ param $值属性值,选项* * @返回抽象组件*/受保护的函数setAttr($name,$ value){ if(array _ key _ exists($ name,$ this-_ attributes)){ unset($ this-_ attributes[$ name]);} $ this-_ attributes[$ name]=$ value;返回$ this} /** *获取组件属性的值* * @访问受保护* * @ param $ name属性名* * @ return string */protected function getAttr($ name){ return array _ key _ exists($ name,$this-_attributes)?$ this-_ attributes[$ name]: null;} /** *设置组件样式* * @访问受保护的* * @param $name样式名称* @ param $值样式值,选项* * @返回抽象组件*/受保护的函数setStyle($name,$ value){ if(array _ key _ exists($ name,$ this-_ style)){ unset($ this-_ style[$ name]);} $ this-_ style[$ name]=$ value;返回$ this} /** *获取组件样式的值* * @访问受保护的* * @param $name属性名称* * @返回字符串*/受保护的函数getStyle($ name){ return array _ key _ exists($ name,$this-_style)?$ this-_ style[$ name]: null;} /** *将组件所有属性转换为字符串,如name=' value ' * * @ access protected * * @ return string */protected function attributeToString(){//$ s=array _ reduce(;$ s=foreach($ this-_属性为$ key=$ value){ $ s .=' $ key=\ ' $ value \ ';}返回$ s;} /** *将组件样式转换为类似样式='的字符串.* * @ access protected * * @ return string */protected function styleToString(){ if(空($ this-_ style))返回"";$ s=foreach($ this-_ style as $ key=$ value){ $ s .=' $ key : $ value;} $ s=' style=\ ' $ s \返回$ s;} /** *设置或获取组件属性* * @访问public * * @param $name属性名称* @ param $值属性值,选项* * @返回string | |抽象组件*/公共函数attr(){ $ name=func _ get _ arg(0);if(func _ num _ args()==1){ return $ this-GetTr($ name);} else if(func _ num _ args()==2){ $ value=func _ get _ arg(1);返回$this-setAttr($name,$ value);} } /** *设置或获取组件样式* * @访问public * * @ param $ name style name * @ param $ value style value,option * * @ return string | |抽象组件*/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);返回$this-setStyle($name,$ value);} } /** *返回超文本标记语言字符串* * @访问公共* * @返回字符串**/公共函数toString(){ thorw New AbstractException('子类必须是覆盖此方法!');} /** *呈现组件* * @访问公众* * @返回void * */public function render(){ echo $ this-ToString();} }