Using OOP technology to optimize PHP applications

Source: Internet
Author: User
Keywords Using OOP technology to optimize PHP applications
Most developers think that the concept of object-oriented programming (OOP) is a contradiction to PHP, but in fact, PHP encapsulates all the features that developers use of OOP technology in their applications. To prove this, let's cite a classic vehicle (vehicle) example with the following PHP classes:
Class Vehicle {
Stuff goes here!
}

In our new vehicle class, we use methods (that is, PHP functions) to contact our application and the attributes of the vehicle class (characteristics). Each method is used to describe the vehicle to the application, so that we can implement some functionality using methods. Consider the following example (it is clearly non-functional, that is, the function body is not given):
class Vehicle {
function getwheels () {
///logically used to query the number of wheels (i.e. wheel) here
return ($w Heels);
}
Function Getdoors () {
///logically used to query the number of doors (i.e., door) here.
return ($wheels);
}
}

Here, we see two "getter" functions. If this looks and sounds familiar, it's because the oop of PHP is very similar to C or Java. The real challenge is the flexible use of OOP, the 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 might 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 a function that returns this information. In PHP, this function can be implemented 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 about the type of vehicle to the vehicle class? To better understand the problem, let's demonstrate the usage, implement our class, and call its Getwheels method to get the number of wheels for a type of vehicle with a car .
//Set Vehicle type
$vehicle = "Car";

So we've announced the type of vehicle we want to create for car. Since we use PHP, this value can be dynamic, but if so, it will soon be difficult to process (Hard-code) it with code.

In order 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 vehicle contains.

Now is the time to find out how many wheels our car has in the end:
Get 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 the vehicle type to it. If all goes well, we get the data we need:
Number of printed wheels
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 code listing b .

Easy to program, easy to manage

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

listing C gives a higher version of the code we discussed here on the automotive classification application. It fully demonstrates the performance of the face object in PHP.

  • Related Article

    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.