PHP Modular programming Skills _php Skills

Source: Internet
Author: User

But it is easy in the UI but somewhat powerless, not only PHP, any Web programming language in the design of the UI have similar problems, host language and HTML mixed in a file, a large number of duplicate HTML code, no technical content, but very time-consuming and laborious. So I would like to be able to do the previous PHP project UI part of the summary and induction, packaged as a small component (like the components in Delphi), the interface is presented as a unified style, you can later write a number of CSS files for this knot component, to provide "skin-changing" function.

All components inherit from the Abatractcomponent class and implement the toString () and render () methods. Abatractcomponent has three main subclasses, one is the container class Continer, which derives from the Panel,poppanel , and Grouppanel class, the second is control class controls , is the parent class of all visual control classes, such as Button,LinkButton class, and the third is the list class list, which implements a UI with lists, name-value pairs.

Abstractcomponent part of the code:

Copy Code code as follows:

<?php
/**
* Component Library
*
* @author Chris Mao
* @package Component
* @description All components must is 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 ' 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 is override this method!!");
}

/**
* Render the component
*
* @access Public
*
* @return void
**/
Public function render () {
echo $this->tostring ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.