PHP componentized programming skills

Source: Internet
Author: User

However, its UI is not convenient, not only PHP, but any webProgramming LanguageThere are similar issues in UI design. The host language and HTML are mixed in a file, and a large number of repeated htmlCode, Without any technical content, but it is very time-consuming and labor-intensive. So I want to summarize and summarize the UI of the PHP project I have previously done, and encapsulate it into small components (just like those in Delphi ), the interface is presented in a unified style. You can write multiple CSS files for this component in the future to provide the "skin-changing" function.

All components are inherited fromAbatractcomponentAnd implementTostring ()AndRender ()Method.AbatractcomponentThere are three main child classes, one of which is the container class.ContinerIs derived fromPanel,PoppanelAndGrouppanelClass. The second is the control class.ControlIs the parent class of all visual control classes, suchButton,LinkbuttonClass. The third is the list class.ListTo implement the UI with list and name-value pairs.

Some codes of abstractcomponent:

Copy code The Code is as follows: <? PHP
/**
* Component Library
*
* @ Author Chris Mao
* @ Package component
* @ Description all components must be extened from the class
* And override the both methods of tostring.
* @ 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 !! ");
}< br> 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"]);
}< br> unset ($ options ["style"]);
}< br> $ this-> _ attributes = array_merge ($ this-> _ attributes, $ options);
}< br> If (! Empty ($ style) & is_array ($ style) {
$ this-> _ style = array_merge ($ this-> _ style, $ style );
}< BR >}

/**
* 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 maid ("subclass must be 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.