Using OOP technology to optimize PHP applications

Source: Internet
Author: User
Tags object contains functions implement php class return
Program | Optimizes most developers think of object-oriented programming (OOP) thinking and PHP as a pair of contradictions, but in fact, PHP encapsulates all the features that developers use OOP technology in their applications. To prove this, let's cite a classic vehicle (vehicle) example, whose PHP class is as follows:
Class Vehicle {
Stuff goes here!
}

In our new vehicle class, use the method (that is, the PHP function) to contact our application and the attributes of the vehicle class (characteristics). Each method is used to describe the vehicle to the application, so we use the method to implement certain functions. Consider the following example (it is clearly not a function, that is, no function body is given):
Class Vehicle {
function Getwheels () {
Logically used to query the number of wheels (ie wheel) here
return ($wheels);
}
function Getdoors () {
Logically used to check the number of doors (i.e. door) here.
return ($wheels);
}
}

Here, we see two "getter" functions. If this sounds familiar, it's because the OOP in PHP is very similar to C or Java. The real challenge is the flexibility to use OOP, full and efficient use of encapsulation.

In order to determine what you need to know in the process of integrating vehicle into your application, it may be helpful to define the basic elements that make up the vehicle. For example, vehicles can be organized according to the number of doors or the number of wheels.

If we want to know how many wheels our vehicle have, we need to return this information to the function. In PHP, the implementation of this function can be as shown in code listing a.

Now our vehicle class contains a method called Getwheels, which returns the number of wheels when the application passes it to a vehicle type. So how do we pass the information of the vehicle type to the vehicle class? To better understand the problem, let's demonstrate usage, implement our class and call its Getwheels method to get the number of wheels for car-type vehicles.
Set the type of vehicle
$vehicle = "Car";

So we've announced that the type of vehicle we want to create is car. Because we use PHP, the value can be dynamic, but it's very difficult to deal with code (HARD-CODE) quickly.

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

We have just created an instance of the vehicle class called Myvehicle. It is the only instance of the vehicle class, and it contains all the data (methods and properties) that the vehicle contains.

Now is the time to find out how many wheels our car has:
To obtain the number of wheels by passing the vehicle type to the Getwheels method
$numWheels = $myVehicle->getwheels ($vehicle);

As you can see, we called the Getwheel method and passed it the vehicle type. If all goes well, we get the data we need:
Number of wheels printed
Print "$vehicle has $numWheels wheels.";

If we want to get the number of doors, we need to write another way to check the number of doors, as shown in Listing B.


Easy to program, easy to manage

As you can see in this simple demo, it's not difficult to implement object-oriented programming in PHP. Creating a shopping cart (shopping cart) is not a hassle, as we use OOP and classes in the vehicle class. Because of the object-oriented programming idea, the code is easy to read, easy to maintain, and easy to upgrade.

Listing C gives us a later version of the code for the car classification application we're talking about here. It fully demonstrates the object-oriented performance of PHP.




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.