• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP 组件化编程技巧_php技巧

php 搞代码 3年前 (2022-01-26) 25次浏览 已收录 0个评论

但其在UI方便却有些力不从心,不仅是PHP,任何一种Web编程语言在设计UI都有类似的问题,宿主语言与HTML混和在一个文件中,大量重复的 HTML代码,毫无任何技术含量,但又非常的费时费力。于是我就希望能够对之前做过的PHP项目UI部分进行总结和归纳,将其封装为一个个小的组件(就像 Delphi中的组件一样),在界面上呈现为统一的风格,日后可以再针对这结组件编写多个CSS文件,提供“换肤”功能。

所有的组件都继承自AbatractComponent这个类,并实现其中的toString()render()方法。AbatractComponent又有三个主要的子类,一个是容器类Continer,其又派生出PanelPopPanelGroupPanel等类,第二个是控件类Control,是所有可视控件类的父类,如ButtonLinkButton等类,第三个则是列表类List,实现有列表,名-值对的UI。

AbstractComponent部分代码:

<?php <BR>/** <BR>* Component Library <BR>* <BR>* @author Chris Mao <BR>* @package Component <BR>* @description All components must be extened from the class <BR>* and override the both methods of toString. <BR>* @copyright Copyright (c) 2009 JueRui Soft Studio <BR>* <BR>**/ <BR>class AbstractComponent { <br><br>/* <BR>* @var _style the component style's array <BR>* <BR>* @access protected <BR>* <BR>*/ <BR>protected $_style = array(); <BR>/* <BR>* @var _attributes the component attribute's string <BR>* <BR>* @access protected <BR>* <BR>*/ <BR>protected $_attributes = array(); <br><br>/** <BR>* constructor function <BR>* <BR>* @access public <BR>* <BR>*/ <BR>public function __construct($options = null, $style = null) { <BR>if (!is_null($options) && (gettype($options) != "array")) { <BR>throw new Exception("The options must be a array!!"); <BR>} <BR>if (!empty($options) && is_array($options)) { <BR>if (array_key_exists("style", $options)) { <BR>if (is_array($options["style"])) { <BR>$this->_style = array_merge($this->_style, $options["style"]); <BR>} <BR>unset($options["style"]); <BR>} <BR>$this->_attributes = array_merge($this->_attributes, $options); <BR>} <BR>if (!empty($style) && is_array($style)) { <BR>$this->_style = array_merge($this->_style, $style); <BR>} <BR>} <br><br>/** <BR>* set the component attributes <BR>* <BR>* @access protected <BR>* <BR>* @param $name attribute name <BR>* @param $value attribute value, option <BR>* <BR>* @return AbstractComponent <BR>*/ <BR>protected function setAttr($name, $value) { <BR>if (array_key_exists($name, $this->_attributes)) { <BR>unset($this->_attributes[$name]); <BR>} <BR>$this->_attributes[$name] = $value; <BR>return $this; <BR>} <br><br>/** <BR>* get the component attributes' value <BR>* <BR>* @access protected <BR>* <BR>* @param $name attribute name <BR>* <BR>* @return string <BR>*/ <BR>protected function getAttr($name) { <BR>return array_key_exists($name, $this->_attributes) ? $this->_attributes[$name] : null; <BR>} <br><br>/** <BR>* set the component style <BR>* <BR>* @access protected <BR>* <BR>* @param $name style name <BR>* @param $value style value, option <BR>* <BR>* @return AbstractComponent <BR>*/ <BR>protected function setStyle($name, $value) { <BR>if (array_key_exists($name, $this->_style)) { <BR>unset($this->_style[$name]); <BR>} <BR>$this->_style[$name] = $value; <BR>return $this; <BR>} <br><br>/** <BR>* get the component style's value <BR>* <BR>* @access protected <BR>* <BR>* @param $name attribute name <BR>* <BR>* @return string <BR>*/ <BR>protected function getStyle($name) { <BR>return array_key_exists($name, $this->_style) ? $this->_style[$name] : null; <BR>} <br><br>/** <BR>* convert the component all attributes to string like name = "value" <BR>* <BR>* @access protected <BR>* <BR>* @return string <BR>*/ <BR>protected function attributeToString() { <BR>//$s = array_reduce(; <BR>$s = ""; <BR>foreach($this->_attributes as $key => $value) { <BR>$s .= " $key=\"$value\" "; <BR>} <BR>return $s; <BR>} <br><br>/** <BR>* convert the component style to string like style = "....." <BR>* <BR>* @access protected <BR>* <BR>* @return string <BR>*/ <BR>protected function styleToString() { <BR>if (empty($this->_style)) return ""; <BR>$s = ""; <BR>foreach($this->_style as $key => $value) { <BR>$s .= " $key: $value; "; <BR>} <BR>$s = " style=\"$s\" "; <BR>return $s; <BR>} <br><br>/** <BR>* set or get the component attributes <BR>* <BR>* @access public <BR>* <BR>* @param $name attribute name <BR>* @param $value attribute value, option <BR>* <BR>* @return string || AbstractComponent <BR>*/ <BR>public function attr() { <BR>$name = func_get_arg(0); <BR>if (func_num_args() == 1) { <BR>return $this->getAttr($name); <BR>} <BR>else if (func_num_args() == 2) { <BR>$value = func_get_arg(1); <BR>return $this->set<p style="color:transparent">。本文来源gao!%daima.com搞$代*!码网1</p><cite>搞代gaodaima码</cite>Attr($name, $value); <BR>} <BR>} <br><br>/** <BR>* set or get the component style <BR>* <BR>* @access public <BR>* <BR>* @param $name style name <BR>* @param $value style value, option <BR>* <BR>* @return string || AbstractComponent <BR>*/ <BR>public function style() { <BR>$name = func_get_arg(0); <BR>if (func_num_args() == 1) { <BR>return $this->getStyle($name); <BR>} <BR>else if (func_num_args() == 2) { <BR>$value = func_get_arg(1); <BR>return $this->setStyle($name, $value); <BR>} <BR>} <br><br>/** <BR>* return the HTML string <BR>* <BR>* @access public <BR>* <BR>* @return string <BR>**/ <BR>public function toString() { <BR>thorw New AbstractException("subclass must be override this method!!"); <BR>} <br><br>/** <BR>* render the component <BR>* <BR>* @access public <BR>* <BR>* @return void <BR>**/ <BR>public function render() { <BR>echo $this->toString(); <BR>} <BR>} <BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP 组件化编程技巧_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址