Understanding and application of classes in PHP [II]

Source: Internet
Author: User
Tags object constructor functions html page modify variables php class variable
Understanding and application of classes in PHP [II]

Many PHP enthusiasts in the learning process to feel the concept of the class in PHP is more difficult to understand and grasp, although know that the class since the existence of the truth, but because the usual contact and use of less opportunities, but also a slight. In fact, as long as we understand the variables and functions of these basic PHP concepts, grasp the meaning of the class is not a problem. Given the important role of classes in PHP, this article will combine specific examples to introduce the concepts and applications of classes in PHP.


A

A class is a collection of variables and functions that act on a variable. class provides a way to describe things in the real world. By using classes effectively, we can combine multiple variables and functions that describe the same object and use them as a whole, making the programs that are written more intuitive, more reasonable, and easier to maintain.

To give a more popular example, we can create a class called Bike to describe a bicycle in real life. First, we set the variables in the class to include pedal $pedals, chain $chain, front-wheel $front wheel, rear-wheel $rear wheel, brake $brakes, and handle $handle. Then, we create a stop stop (), accelerate accelerate (), turn left TurnLeft (), and turn right turnright () and other functions. In this way, we have a class that can describe all the behavior and attributes of the bike object. For example, we can pass the $front wheel and $rear wheel variables to the TurnLeft () function, which results in some sort of output.

Some people may ask the above example although interesting, but we can use the normal variables and functions to achieve the same function, why must insist on using such a trivial class? Of course, if we only need to describe a bicycle in a script, defining a class does not seem to have any special value. But what if a script needs to involve more than one bike? If we still use the usual method of defining variables and functions for each bike, it is quite complicated to keep track of each variable and make sure that the correct variable is entered in the correct function. Conversely, if you adopt a class approach, you can effectively reduce the number of variables you need. In addition, a class that has already been defined can be included in other files or scripts to enable reuse of the code.


Two

After understanding the concept of the PHP class, let's take a look at how to create and use classes in the script.

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

First, we create a class named style with the following code, and define the variables needed to set the page properties in the class:

<?php

Class Style {

var $text;

var $alink;

var $vlink;

var $link;

var $bgcol;

var $face;

var $size;

var $align;

var $valign;

}

?>

Readers who are familiar with the HTML language will not be unfamiliar with the variables we have known 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:

<?php

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;

}

}

?>

Description

You can use "$this" in a class to refer to the class itself, using the "->" operator to refer to each variable or function in the class. A function that is created in a class with the same name as a class is called a constructor. The constructor executes automatically when a new object instance is created, thereby assigning the default value set in the class to the object instance.

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

<?php $Instance _name = new Class;?>

For example, in this example we can use the following code to create an object instance named $basic that automatically obtains the default value specified by the constructor in the style class:

<?php $Basic = new Style;?>

A class can create multiple object instances in which each object instance inherits the default properties of the class, and can invoke all variables and functions in the class. Sometimes we may need to set different properties for multiple object instances created by the same class. Although we can modify the default value of a class when we create an object instance, this approach lacks flexibility and, on the other hand, according to PHP's convention, if a user declares a variable value when creating a new object instance, You must also declare all variables after the variable declared in the class. For example, if we explicitly modify the value of the text variable when we create an instance of the $basic object, we must also declare all the variables that are in the class after the text variable, that is, all the variables of the style class need to be declared again. Obviously, we need to modify a variable value in an object instance in a more convenient way. Here, we can do this by creating a function in the style class that specifically modifies the value of the variable. The specific code is as follows:

Function Set ($varname, $value) {

$this-> $varname = $value;

}

For example, if we need to change the value of the size variable in the $basic object instance to 2 o'clock, use the following code to implement:

<?php $Basic->set (' size ', 2);?>

Description

The-> operator is used to describe the set () function in the running $basic object instance to the PHP interpreter.

By using the set () function effectively, we have the flexibility to set up and control different object instances. For example, if we want to set a different background color and font size for the header row and the content row of the page output table, you can first create two object instances and then use the set () function to set each of the different properties. The specific code is as follows:

<?php

$Tableheader = new Style;

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

$Tableheader->set (' Bgcol ', ' #000000 ');

?>

<?php

$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.