PHP component programming skills_PHP tutorial

WBOY
Release: 2016-07-21 15:46:05
Original
797 people have browsed it

But it is somewhat inadequate in terms of UI convenience. Not only PHP, but any web programming language has similar problems when designing UI. The host language and HTML are mixed in one file, with a large number of repeated HTML codes, without any Technical content, but very time-consuming and labor-intensive. So I hope to be able to summarize and summarize the UI part of the PHP project I have done before, encapsulate it into small components (just like the components in Delphi), and present them in a unified style on the interface, so that I can use them again in the future. Write multiple CSS files for this knot component to provide the "skinning" function.

All components inherit from the AbatractComponent class and implement toString() and render()Method. AbatractComponent has three main subclasses. One is the container class Continer, which in turn derives Panel, PopPanel and GroupPanel and other classes, the second is the control class Control, which is the parent class of all visual control classes, such as Button, LinkButton and other classes, The third one is the list class List, which implements a UI with lists and name-value pairs.

继承图

AbstractComponent part code:

Copy code The code is as follows:

/**
* 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();
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320251.htmlTechArticleBut its UI convenience is somewhat inadequate. Not only PHP, but any web programming language has its own advantages in designing UI. Similar problem, host language and HTML are mixed in one file, a lot of repetition...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!