PHP class understanding and application [2] _ PHP Tutorial

Source: Internet
Author: User
Understanding and application of classes in PHP [2]. Understanding and application of classes in PHP [2] Many PHP fans feel difficult to understand and master the concept of classes in PHP during their learning process. although they know that classes exist, there is a truth, however, PHP class understanding and application [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.

Many PHP fans feel that they are hard to understand and master the concept of classes in PHP during their learning process. although they know that classes already exist, there is a truth,...

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.