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 Vehicle class, we use methods (namely PHP functions) to contact most developers who think that the idea of Program Design (OOP) for objects is a conflict with that of 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 as follows:Code List.
Now our Vehicle class containsGetWheelsWhen the application passes it a vehicle type, it returns the number of wheels. 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 itsGetWheelsMethod to obtainCarThe number of wheel types.
// 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, suchCode 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.
List CThis section describes the code of a later version of the automotive classification application. It fully demonstrates the performance of PHP surface objects.