With the enhancement of PHP's support for OOP and the development of programming ideas, more and more PHP programmers have entered the OOP world. Through simple learning, most programmers can quickly master the basic skills of OOP programming. In the code written by programmers, there are more and more strings such as class and new. But as a programmer, do you fall into the programming trap of an OO form and PO idea?
With the enhancement of PHP's support for OOP and the development of programming ideas, more and more PHP programmers have entered the OOP world. Through simple learning, most programmers can quickly master the basic skills of OOP programming. In the code written by programmers, there are more and more strings such as class and new. But as a programmer, do you fall into the programming trap of an OO form and PO idea?
As a programmer in the POP age, to calculate the price of all the doors of a house, they will write such a function.
/*************************************** * ** Function: priceOfHouseDoor ($ num, $ price) function: calculates the total price of all doors. parameter: $ num number of doors, $ price: The price of each door ********************************* * *******/function priceHouseDoor ($ num, $ price) {return $ num * $ price;} echo priceHouseDoor (2, 5); // output price
Or let's just make a more process:
$ Doors = 2; // number of doors $ price = 5; // echo $ doors * $ price for each door; // output price
This is a typical POP. After learning about OOP, many people may change the code that implements this function. Maybe programmers write this:
Classhouse {/************************************* * *** function: getDoorPrice ($ num, $ price) function: calculates the total price of all doors. parameter: $ num number of doors, $ price: The price of each door ********************************* * *******/functiongetDoorPrice ($ num, $ price) {return $ num * $ price ;}$ house = newhouse; echo $ house-> getDoorPrice (); // output price
We can see at once that this code uses the representative of OOP-class and instantiation. So is this object-oriented programming?
I will take a closer look and find that this class is actually just the object-oriented form of the priceOfHouseDoor ($ num, $ price) function above.
Well, now let's first look at what is object-oriented programming. In my understanding, object-oriented programming has two core contents. Class 1 and instance 2. Class is the abstraction of a class of things, and instance is the individual in this class of things. The attributes (values) and methods (functions) of the instance are the specific content and actions of the individual.
Let's take a look at the house class above. It has a method, getDoorPrice ($ num, $ price), which introduces two parameters. One is the number of houses and the other is the price of houses. We found that when we use this method, these two values are built temporarily and there is no connection with the house. In real life, the door in a house has no connection with the house, which is incomprehensible. As we know, OOP is actually a microcosm of our real world. Therefore, the house in OOP has no connection with the house door, and it is not understandable. it is a non-OO thinking.
So how should we use real OO to implement the above functions? See the following code:
Classhouse {var $ doors; // number of doors var $ pricePerDoors; // functionsetNumOfDoors ($ num) {$ this-> doors = $ num ;} // set the number of doors to functionsetPricePerDoor ($ price) {$ this-> pricePerDoors = $ price ;} // set the price of each door *\********** * **/functiongetDoorPrice () {return ($ this-> doors) * ($ this-> pricePerDoors) ;}}$ house = newhouse; $ house-> setNumOfDoors (2 ); // set the number of doors $ house-> setPricePerDoor (5); // set the price of each door echo $ house-> getDoorPrice (); // output
We can clearly see that the number of doors and price houses of the above code have already been combined. This is the real object-oriented approach.
This article is available at http://www.nowamagic.net/librarys/veda/detail/4262.