Understanding and application of classes in PHP

Source: Internet
Author: User
Many PHP fans feel that they are difficult to understand and master the concept of classes in PHP during their learning process. although they know that classes already exist, however, since there are few opportunities for access and use at ordinary times, this is just a little too much. In fact, as long as we understand the basic PHP concepts of variables and functions, it is not a problem to grasp the meaning of classes. In view of the important role of classes in PHP, this article makes it difficult for many PHP fans to understand and master the concept of classes in PHP, although you know that a class exists, it is omitted because there are fewer contacts and use opportunities at ordinary times. In fact, as long as we understand the basic PHP concepts of variables and functions, it is not a problem to grasp the meaning of classes. In view of the important role of classes in PHP, this article will introduce the concept and application of classes in PHP based on specific examples.


(1)

A class is a set of variables and functions acting on variables. Class provides a way to describe things in the real world. By effectively using classes, we can combine multiple variables and functions that describe the same object as a whole, so that the compiled program is more intuitive and reasonable, easy to maintain.

For example, we can create a class named Bike to describe a bicycle in real life. First, we set the variables in this class to include pedal $ pedals, chain $ chain, front wheel $ front wheel, rear wheel $ rear wheel, brake $ brakes, and handle $ handle. Then, we create functions such as Stop (), Accelerate (), left turn TurnLeft (), and right turn TurnRight. In this way, we have a class that describes all the behaviors and attributes of the bicycle object. For example, we can pass $ front wheel and $ rear wheel variables into the TurnLeft () function to get some output result.

Some may ask if the above example is interesting, but we can use common variables and functions to implement the same function. why must we stick to such a cumbersome class? Of course, if we only need to describe a bicycle in the script, defining a class does not seem to have any special value. But what if the script involves multiple bicycles? If we still use the conventional method of defining variables and functions for each bicycle, it is quite complicated to track every variable and ensure that the correct input of the variable is correct. On the contrary, if you use a class, you can effectively reduce the number of required variables. In addition, a defined class can be included in other files or scripts to reuse the code.


(2)

After learning about the concept of PHP classes, let's take a look at how to create and use classes in scripts.

I believe that when you create a site, you must take into account the page display effect, font style, size, and other details and affect the design and conception of the entire page layout and the entire site process. Next, we will use PHP classes to control HTML page output.

First, use the following code to create a class named Style and define the variables required to set page properties in the class:


Class Style {

Var $ text;

Var $ alink;

Var $ vlink;

Var $ link;

Var $ bgcol;

Var $ face;

Var $ size;

Var $ align;

Var $ valign;

}

?>

I believe that readers familiar with the HTML language will not be unfamiliar with the variables whose names are in the Style class.

Next, we create a function with the same name as the class in the Style class. The code is as follows:


Class Style {

Function Style ($ text = "#000000", $ alink = "# AA00AA", $ vlink = "# AA00AA ",

$ Link = "# 3333FF", $ bgcol = "#999999", $ face = "Arial", $ size = 3,

$ Align = "CENTER", $ valign = "TOP ")

{

$ This-> text = $ text;

$ This-> alink = $ alink;

$ This-> vlink = $ vlink;

$ This-> link = $ link;

$ This-> bgcol = $ bgcol;

$ This-> face = $ face;

$ This-> size = $ size;

$ This-> align = $ align;

$ This-> valign = $ valign;

}

}

?>

Note:

In a class, you can use "$ this" to refer to the class itself and use the "->" operator to reference various variables or functions in the class. A function created in a class with the same name as a class is called a constructor. The constructor is automatically executed when a new object instance is created, so that the default value set in the class is assigned to the object instance.

In PHP, we can use a defined class by creating an object instance. The syntax format is as follows:

  

For example, in this example, we can use the following code to create an object instance named $ Basic. this object instance will automatically obtain the default value specified by the constructor in the Style class:

  

A class can create multiple object instances. each object instance inherits the default attributes of the class and can call all variables and functions in the class. Sometimes we may need to set different attributes for multiple object instances created for the same class. although we can modify the default value of the class when creating the object instance, this method lacks flexibility, on the other hand, according to PHP, if you declare a variable value when creating a new object instance, you must declare all the variables after the variables declared in this class. For example, if we explicitly modify the text variable value when creating a $ Basic object instance, we must declare all the variables in the class after the text variable, that is, all variables of the Style class must be re-declared. Obviously, we need to modify a variable value in the object instance in other more convenient ways. Here, we can create a function specifically used to modify the variable value in the Style class to implement the above functions. The code is as follows:

Function Set ($ varname, $ value ){

$ This-> $ varname = $ value;

}

For example, if you want to change the value of the size variable in the $ Basic object instance to 2, use the following code:

Set ('size', 2);?>

Note:

-> The operator is used to explain to the PHP interpreter how to run the Set () function in the $ Basic object instance.

By effectively using the Set () function, we can flexibly Set and control different object instances. For example, if we want to Set different background colors and font sizes for the title row and content row of the page output table, we can first create two object instances and then use Set () the functions set different attributes respectively. The code is as follows:


$ Tableheader = new Style;

$ Tableheader-> Set ('text', '# 0000FF ');

$ Tableheader-> Set ('bgcol ',' #000000 ');

?>


$ Tablecontent = new Style;

$ Tablecontent-> Set ('bgcol ',' # AAAAAA ');

$ Tablecontent-> Set ('size', 2 );

?>
Many PHP fans feel that they are difficult to understand and master the concept of classes in PHP during their learning process. although they know that classes already exist, however, since there are few opportunities for access and use at ordinary times, this is just a little too much. In fact, as long as we understand the basic PHP concepts of variables and functions, it is not a problem to grasp the meaning of classes. In view of the important role of classes in PHP, this article will introduce the concept and application of classes in PHP based on specific examples.


(1)

A class is a set of variables and functions acting on variables. Class provides a way to describe things in the real world. By effectively using classes, we can combine multiple variables and functions that describe the same object as a whole, so that the compiled program is more intuitive and reasonable, easy to maintain.

For example, we can create a class named Bike to describe a bicycle in real life. First, we set the variables in this class to include pedal $ pedals, chain $ chain, front wheel $ front wheel, rear wheel $ rear wheel, brake $ brakes, and handle $ handle. Then, we create functions such as Stop (), Accelerate (), left turn TurnLeft (), and right turn TurnRight. In this way, we have a class that describes all the behaviors and attributes of the bicycle object. For example, we can pass $ front wheel and $ rear wheel variables into the TurnLeft () function to get some output result.

Some may ask if the above example is interesting, but we can use common variables and functions to implement the same function. why must we stick to such a cumbersome class? Of course, if we only need to describe a bicycle in the script, defining a class does not seem to have any special value. But what if the script involves multiple bicycles? If we still use the conventional method of defining variables and functions for each bicycle, it is quite complicated to track every variable and ensure that the correct input of the variable is correct. On the contrary, if you use a class, you can effectively reduce the number of required variables. In addition, a defined class can be included in other files or scripts to reuse the code.


(2)

After learning about the concept of PHP classes, let's take a look at how to create and use classes in scripts.

I believe that when you create a site, you must take into account the page display effect, font style, size, and other details and affect the design and conception of the entire page layout and the entire site process. Next, we will use PHP classes to control HTML page output.

First, use the following code to create a class named Style and define the variables required to set page properties in the class:


Class Style {

Var $ text;

Var $ alink;

Var $ vlink;

Var $ link;

Var $ bgcol;

Var $ face;

Var $ size;

Var $ align;

Var $ valign;

}

?>

I believe that readers familiar with the HTML language will not be unfamiliar with the variables whose names are in the Style class.

Next, we create a function with the same name as the class in the Style class. The code is as follows:


Class Style {

Function Style ($ text = "#000000", $ alink = "# AA00AA", $ vlink = "# AA00AA ",

$ Link = "# 3333FF", $ bgcol = "#999999", $ face = "Arial", $ size = 3,

$ Align = "CENTER", $ valign = "TOP ")

{

$ This-> text = $ text;

$ This-> alink = $ alink;

$ This-> vlink = $ vlink;

$ This-> link = $ link;

$ This-> bgcol = $ bgcol;

$ This-> face = $ face;

$ This-> size = $ size;

$ This-> align = $ align;

$ This-> valign = $ valign;

}

}

?>

Note:

In a class, you can use "$ this" to refer to the class itself and use the "->" operator to reference various variables or functions in the class. A function created in a class with the same name as a class is called a constructor. The constructor is automatically executed when a new object instance is created, so that the default value set in the class is assigned to the object instance.

In PHP, we can use a defined class by creating an object instance. The syntax format is as follows:

  

For example, in this example, we can use the following code to create an object instance named $ Basic. this object instance will automatically obtain the default value specified by the constructor in the Style class:

  

A class can create multiple object instances. each object instance inherits the default attributes of the class and can call all variables and functions in the class. Sometimes we may need to set different attributes for multiple object instances created for the same class. although we can modify the default value of the class when creating the object instance, this method lacks flexibility, on the other hand, according to PHP, if you declare a variable value when creating a new object instance, you must declare all the variables after the variables declared in this class. For example, if we explicitly modify the text variable value when creating a $ Basic object instance, we must declare all the variables in the class after the text variable, that is, all variables of the Style class must be re-declared. Obviously, we need to modify a variable value in the object instance in other more convenient ways. Here, we can create a function specifically used to modify the variable value in the Style class to implement the above functions. The code is as follows:

Function Set ($ varname, $ value ){

$ This-> $ varname = $ value;

}

For example, if you want to change the value of the size variable in the $ Basic object instance to 2, use the following code:

Set ('size', 2);?>

Note:

-> The operator is used to explain to the PHP interpreter how to run the Set () function in the $ Basic object instance.

By effectively using the Set () function, we can flexibly Set and control different object instances. For example, if we want to Set different background colors and font sizes for the title row and content row of the page output table, we can first create two object instances and then use Set () the functions set different attributes respectively. The code is as follows:


$ Tableheader = new Style;

$ Tableheader-> Set ('text', '# 0000FF ');

$ Tableheader-> Set ('bgcol ',' #000000 ');

?>


$ Tablecontent = new Style;

$ Tablecontent-> Set ('bgcol ',' # AAAAAA ');

$ Tablecontent-> Set ('size', 2 );

?>

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.