Home >Backend Development >PHP Tutorial >Use OOP technology to optimize PHP applications_PHP tutorial

Use OOP technology to optimize PHP applications_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:26:36929browse

Most developers think that object-oriented programming (OOP) thinking and PHP are contradictory, but in fact, PHP encapsulates all the functions that developers can use OOP technology in their applications. To prove this, let us take a classic Vehicle example, whose PHP class is as follows: class Vehicle { // Stuff goes here! } In our new Vehicle class, use methods (ie PHP functions) to contact us Characteristics of the Application and Vehicle classes. Each method is used to describe the Vehicle to the application, so that we use the method to implement certain functions. Please consider the following example (it is obviously non-functional, that is, no function body is given): class Vehicle { function getWheels() { // Logically used to query the number of wheels here return($wheels); } function getDoors() { // Logically used to query the number of car doors here. return($wheels); } } Here, we see two "getter" functions. If this looks and sounds familiar, that's because PHP's OOP is very similar to C or Java. The real challenge is to use OOP flexibly and to use encapsulation fully and effectively. To determine what you need to know about integrating a Vehicle into your application, it may be helpful to define the basic elements that make up a Vehicle. 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 shown in the code listing A. Our Vehicle class now contains a method called getWheels, which returns the number of wheels when the application passes it a vehicle type. So, how do we pass the vehicle type information to the Vehicle class? In order to better understand this problem, let us demonstrate the usage, implement our class and call its getWheels method to get the number of wheels of the car type vehicle. //Set the vehicle type $vehicle = "car"; In this way, we have declared the vehicle type we want to create as car. Since we're using PHP, this value can be dynamic, but then it quickly becomes hard-coded. In order 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 of the Vehicle class called myVehicle. It is the only instance of the Vehicle class, and it contains all the data (methods and properties) contained in the Vehicle. Now it's time to find 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 called getWheel method and pass it the vehicle type. If all goes well, we 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, such as code Listing B shown. Easy to program, easy to manage As you can see in this simple demonstration, implementing object-oriented programming in PHP is not difficult. Using OOP and classes like we did in the Vehicle class, creating a shopping cart is not a huge hassle. Due to the adoption of object-oriented programming ideas, the code is easy to read, maintain, and upgrade. Listing C gives the code for a later version of the car classification application we're discussing here. It fully demonstrates the object-oriented performance of PHP.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531948.htmlTechArticleMost developers think that object-oriented programming (OOP) thinking and PHP are contradictory, but in fact , PHP encapsulates all the functionality for developers to use OOP technology in their applications. For...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn