Understanding and Using PHP classes

Source: Internet
Author: User
Many PHP fans feel difficult to understand and grasp the concept of class in PHP during their learning process. although they know that class already exists, however, since there are few contacts and application opportunities at ordinary times, this is just a little too much. Actually, as long as we know that many PHP fans feel that it is difficult to understand and grasp the concept of classes in PHP during their learning process, we certainly know that classes have their own reasons since they exist, however, since there are few contacts and application opportunities at ordinary times, this is just a little too much. Actually, as long as we understand the basic PHP concepts of variables and functions, we cannot grasp the meaning of classes. In view of the important role of classes in PHP, this article will first introduce the concept and use of classes in PHP in combination with specific examples.


(1)

A class is a collection composed of variables and functions acting on variables. Class provides a way to describe things in the real world. Through effective application classes, we can combine multiple variables and functions describing the same object as a whole application, so that the compiled program is more intuitive and fair, easy to protect.

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 the 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 (), turn left TurnLeft (), and right turn TurnRight. In this way, we have a class that describes all the actions 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.

Some may ask why the above example is interesting, but we can apply the conventional variables as well as the function implementation. why must we keep the application so cumbersome? Of course, if we only need to describe a bicycle in the script, it seems that defining a class has no special value. But what if the script involves multiple bicycles? If we still adopt the conventional method of defining variables and functions for each bicycle, it is quite complicated to track every variable and ensure accurate variable input into the function. On the contrary, if the class method is used, the number of required variables can be effectively reduced. In addition, a defined class can be included in other files or scripts to reuse the code.


(2)

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

We trust that when you create a website, you must consider the page display effect, font style and size, and other details, which affect the design and conception of all page layout and all site processes. Next, we will use the PHP class to control the HTML page output.

First, we 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;

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.