Optimize PHP applications using OOP technology

Source: Internet
Author: User
Tags php oop
Most developers think that the object-oriented programming (OOP) idea is a conflict with PHP, but in fact, PHP encapsulates all the functions of the developer to use OOP technology in their applications. To prove this, let's take a classic Vehicle (Vehicle) example. its PHP class is as follows: classVehicle {Stuffgoeshere !} In our new Vehicl, most developers think that the program design (OOP) idea of the object is a conflict with PHP, but in fact, PHP encapsulates all the functions that developers use OOP technology in their applications. To prove this, let's take a classic Vehicle (Vehicle) example. its PHP class is as follows:
Class Vehicle {
// Stuff goes here!
}

In our new Vehicle class, we use methods (namely PHP functions) to contact our applications and the features of the Vehicle class (characteristics ). Every method is used to describe Vehicle to the application, so that we can use methods to implement certain functions. Consider the following example (it is obviously non-functional, that is, the function body is not given ):
Class Vehicle {
Function getWheels (){
// Logically used to query the number of wheels (that is, the number of wheels) here
Return ($ wheels );
}
Function getDoors (){
// Logically, it is used to query the number of doors.
Return ($ wheels );
}
}

Here, we see two "getter" functions. If this looks and sounds familiar, it is because php oop is very similar to C or Java. The real challenge is flexible use of OOP, full and effective use of encapsulation.

To determine what content you need to understand when integrating Vehicle into your application, defining the basic elements that make up Vehicle may be helpful. For example, Vehicles can be organized by the number of doors or the number of wheels.

If we want to know how many wheels our Vehicle has, we need a function that returns this information. In PHP, the implementation of this function can be shown in Code List.

Now our Vehicle class contains a getWheels method, which returns the number of wheels when an application passes to a Vehicle type. So how can we transmit the Vehicle type information to the Vehicle class? To better understand this problem, Let's demonstrate its usage, implement our class, and call its getWheels method to get the number of wheels of a car.
// Set the vehicle type
$ Vehicle = "car ";

In this way, we have announced the vehicle type we want to create as car. Because we use PHP, this value can be dynamic, but it will soon be difficult to process it with code.

To use this class, we first need to create an instance of this class in the application:
// Create a new instance of the car class
$ MyVehicle = & new Vehicle;

We just created an instance named myVehicle's Vehicle class. It is a unique instance of the Vehicle class and contains all the data (methods and attributes) contained by Vehicle ).

Now is the time to figure out how many wheels our car has:
// Get the number of wheels by passing the vehicle type to the getWheels method
$ NumWheels = $ myVehicle-> getWheels ($ vehicle );

As you can see, we call the getWheel method and pass it the vehicle type. If everything goes well, we will get the data we need:
// Print the number of wheels
Print "$ vehicle has $ numWheels wheels .";

If we want to get the number of doors, we need to write another method to check the number of doors, as shown in Code List B.


Easy to program and manage

As you can see in this simple demonstration, it is not difficult to program surface objects in PHP. Using OOP and classes as we do in the Vehicle class will not be very troublesome to create a shopping cart. Because of the use of object-oriented programming ideas, the code is easy to read, easy to maintain, and easy to upgrade.

Listing C provides the code for a later version of the car classification application that we have discussed here. It fully demonstrates the performance of PHP surface objects.

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.