首页 php教程 PHP开发 PHP 组件化编程技巧

PHP 组件化编程技巧

Dec 14, 2016 pm 01:09 PM
php 技巧 编程

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

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

 

AbstractComponent部分代码:
/**
* 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中文网(www.php.cn)!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

您如何在PHP中解析和处理HTML/XML? 您如何在PHP中解析和处理HTML/XML? Feb 07, 2025 am 11:57 AM

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

php程序在字符串中计数元音 php程序在字符串中计数元音 Feb 07, 2025 pm 12:12 PM

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

编码的关键:为初学者释放 Python 的力量 编码的关键:为初学者释放 Python 的力量 Oct 11, 2024 pm 12:17 PM

Python通过其易学性和强大功能,是初学者的理想编程入门语言。其基础包括:变量:用于存储数据(数字、字符串、列表等)。数据类型:定义变量中数据的类型(整数、浮点数等)。运算符:用于数学运算和比较。控制流:控制代码执行流(条件语句、循环)。

使用 Python 解决问题:作为初学者,解锁强大的解决方案 使用 Python 解决问题:作为初学者,解锁强大的解决方案 Oct 11, 2024 pm 08:58 PM

Python 使初学者能够解决问题。其用户友好的语法、广泛的库以及变量、条件语句和循环等功能可实现高效的代码开发。从管理数据到控制程序流程和执行重复任务,Python 提供了

我后悔之前不知道的 7 个 PHP 函数 我后悔之前不知道的 7 个 PHP 函数 Nov 13, 2024 am 09:42 AM

如果您是一位经验丰富的 PHP 开发人员,您可能会感觉您已经在那里并且已经完成了。您已经开发了大量的应用程序,调试了数百万行代码,并调整了一堆脚本来实现操作

揭秘 C:为新程序员提供一条清晰简单的道路 揭秘 C:为新程序员提供一条清晰简单的道路 Oct 11, 2024 pm 10:47 PM

C是一种初学者学习系统编程的理想选择,它包含以下组件:头文件、函数和主函数。一个简单的C程序可以打印“HelloWorld”,需要包含标准输入/输出函数声明的头文件,并在主函数中使用printf函数来打印。通过使用GCC编译器可以编译和运行C程序。掌握基础后,可以继续学习数据类型、函数、数组和文件处理等主题,以成为熟练的C程序员。

See all articles