A php class tree (supports unlimited classification)

Source: Internet
Author: User
When you declare a class, you need to list all variables and all functions of the object that are called attributes and methods. 3.1.1 shows the composition of a class. note that you can only declare variables or functions in braces. 3.1.2 shows how to define three attributes and two methods in a class. 3.1.1classNameextendsAnotherClass {AccessVariableD when you declare a class, you need to list all variables and all functions of the object-called attributes and methods. 3.1.1 shows the composition of a class. note that you can only declare variables or functions in braces. 3.1.2 shows how to define three attributes and two methods in a class.

3.1.1

Class Name extends Another Class
{
Access Variable Declaration
Access Function Declaration
}

3.1.2

// Define a class for tracking users
Class User
{
// Attributes
Public $ name;
Private $ password, $ lastLogin;

// Method
Public function _ construct ($ name, $ password)
{
$ This-> name = $ name;
$ This-> password = $ password;
$ This-> lastLogin = time ();
$ This-> accesses ++;
}

// Obtain the last access time
Function getLastLogin ()
{
Return (date ("M d Y", $ this-> lastLogin ));
}
}

// Create an object instance
$ User = new User ("Leon", "sdf123 ");

// Obtain the last access time
Print ($ user-> getLastLogin ()."
N ");

// Print the user name
Print ("$ user-> name
N ");
?>

When you declare an attribute, you do not need to specify the data type. the variable may be an integer, string, or another object, depending on the actual situation. it is a good idea to add a comment when declaring an attribute. it indicates the meaning and data type of the attribute.

When you declare a method, what you do is the same as defining a function outside the class. methods and attributes have their own namespaces. this means you can safely create a method with the same name as an external function of the class, and the two do not conflict. for example, a class can define a method named date. however, you cannot name a method as a PHP keyword, such as for or while.

Class methods may contain the so-called type hint in PHP. type hint is the name of the class passed to the method by another parameter. if your script calls a method and passes a variable that is not an instance of the class, PHP will generate a "fatal error ". you may not give type hint to other types, such as integer, string, or boolean value. at the time of writing, whether the type hint should contain the array type is still controversial.

Type hint is a shortcut to test the data Type of an instance of function parameters or operators. you may always return this method. confirm which data type you want to force a parameter to be, such as an integer. 3.2.1 make sure that the compilation class only generates widgets.

3.2.1

// Component
Class Widget
{
Public $ name = 'none ';
Public $ created = FALSE;
}

// Assembler
Class cycler
{
Public function make (Widget $ w)
{
Print ("Making $ w-> name
N ");
$ W-> created = TRUE;
}
}

// Create a Component Object
$ Thing = new Widget;
$ Thing-> name = 'gadget ';

// Assemble components
Else ER: make ($ thing );
?>

In addition to passing parameter variables, the method contains a special variable. it represents an individual instance of the class. you should use this to point to the object attributes and other methods. some object-oriented languages assume that an unqualified variable is submitted to the local attribute, but any variable of the method in PHP is only within a certain scope of the method. note the usage of this variable in the User class constructor (3.1.2 ).

PHP defines an access restriction before the attribute and method declaration, such as public, private, and protected. in addition, you can use "static" to mark a member. you can also declare constants in the class. this chapter will discuss different access methods later.

You can list multiple class tree questions recently asked by the same accessors in a row, so I'm glad I wrote one. I just wrote it and you can use it for reference, see how it can be implemented faster and simpler. paste your tree (only query the database once)

  
This is a category tree in the category list and supports unlimited classification.

A classification can contain both "classification containing subclasses" and "final classification ";

  


The only advantage is that you only need to perform a database query once.

  
It looks pretty bad, but it can be customized and modified. you can customize css and add it.

  
The cache has not been completed. you can add it by yourself.
  
The directory structure in the following example is as follows.
Usage -- Catagory. php

Tags -- images ---- tree. jsp

Response --images----treeopen.gif

Response --images----treeclose.gif

Export --images----line.gif

  
/***************** Tree. jsp ********************/
Function expand (id ){
Node = document. all ('node' + id );
If (node. style. display = ''){
Node. style. display = 'none ';
Document. images ('IMG '+ id). src = imgopen;
} Else {
Node. style. display = '';
Document. images ('IMG '+ id). src = imgclose;
}
}
  
/***************** Catagory. php ********************/
     
Define ('catagory _ TREE_EXPEND_NONE ', 0 );
Define ('catagory _ TREE_EXPEND_ALL ', 1 );
  
Class Catagory {
// Basic classification data
Var $ treeData = array ();
// Classification level structure array, with the classification id value as the key word of the array
Var $ treePList = array ();
// Relationship between the self-classification class and the parent class
Var $ treeCList = array ();
/*
* This is a big classification template.
*
* _ Id__ category number
* _ Name _ category name
* _ Image _ name of the image displayed before classification $ imgOpen or $ imgClose
* _ Open _ indicates whether the category is expanded.
* _ Inner _ position of subcategory display

  

* Styles can be modified as needed, but existing elements cannot be deleted.
*/
Var $ blockTpl ='
  








                 

 
 
_ Name __
_ Inner __
';
/*
* This is a small classification template.
*
* See $ blockTpl
*/
Var $ elementTpl = '_ name __
';
/*
* This is the current position display template.
*
* See $ blockTpl
*/
Var $ currentTpl = '_ name __';
Var $ js = "images/tree. js ";
  
Var $ imgOpen = 'Images/treeopen.gif ';
Var $ imgClose = 'Images/treeclose.gif ';
Var $ imgLine = 'Images/line.gif ';
  
Var $ cachFile = '';
Var $ expand = 0;
  
Var $ result = array ();
Var $ treeStr = '';
Var $ currentStr = '';
/*
* Used for initialization and passed in classified data
*
* Param $ data array ()
*/
Function Catagory (& $ data ){
$ This-> _ init ($ data );
}
    
Function _ init ($ tmpData ){
$ Plevel = $ clevel = $ treeData = array ();

  

Foreach ($ tmpData as $ value ){
$ TreeData [$ value ['id'] = $ value;
$ Plevel [$ value ['pid'] [$ value ['id'] = 'end ';
$ Clevel [$ value ['id'] = $ value ['pid '];
}
$ This-> treeData = & $ treeData;
$ This-> treePList = & $ plevel;
$ This-> treeCList = & $ clevel;
}
/*
* Parse the category list
*
* Param $ cataId int ID of the primary category to be parsed
*/
Function parseNode ($ cataId = 0 ){
$ This-> result = $ this-> treePList [$ cataId];
If ($ this-> result = null) die ("Catagory id error ");
$ This-> treeStr = $ this-> _ doNode ($ this-> result );
$ This-> treeStr. = $ this-> _ jsParse ();
}
  
Function & _ doNode (& $ result ){
$ Nstr = $ estr = '';
Foreach ($ result as $ key => $ value ){
If (isset ($ this-> treePList [$ key]) {
$ Result [$ key] = $ this-> treePList [$ key];
$ Inner = $ this-> _ doNode ($ result [$ key]);
$ Nstr. = $ this-> _ parseNodeTpl ($ key, $ inner );
} Else {
$ Estr. = $ this-> _ parseElementTpl ($ key );
}
}
Return $ nstr. $ estr;
}
    
Function & _ parseNodeTpl ($ cataId, $ inner ){
$ Data = $ this-> treeData [$ cataId];
$ Str = preg_replace ('your _ id _ region', $ data ['id'], $ this-> blockTpl );
$ Str = preg_replace ('your _ name _ secret', $ data ['name'], $ str );
$ Str = preg_replace ('prop _ image _ replation', ($ this-> expand? $ This-> imgClose: $ this-> imgOpen), $ str );
$ Str = preg_replace ('prop _ open _ replation', ($ this-> expand? '': 'None'), $ str );
$ Str = preg_replace ('your _ inner _ region', $ inner, $ str );
Return $ str;
}
  
Function _ parseElementTpl ($ cataId ){
$ Data = $ this-> treeData [$ cataId];
$ Str = preg_replace ('your _ id _ region', $ data ['id'], $ this-> elementTpl );
$ Str = preg_replace ('your _ name _ secret', $ data ['name'], $ str );
$ Str = preg_replace ('your _ image _ users', $ this-> imgLine, $ str );
Return $ str;
}


  


Function _ jsParse (){
$ Str ="

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.