Avoiding the form of OOP and the idea of POP _ PHP Tutorial

Source: Internet
Author: User
Avoid the idea of POP in the form of OOP. To avoid the form of OOP, I haven't published a technical article on the idea of POP for a long time. today I saw a brother posted this post in 21PHP and turned it over. some of the methods used to enhance OOP learning are -------------------- avoiding the idea of OOP and POP.
I haven't published a technical article for a long time. today, I saw a brother posting this post in 21PHP and turned it over. it is useful for strengthening OOP learning.
----------------------------------------------------------
Avoid OOP forms and POP ideas
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. the number of classes, new and other strings in the code written by programmers is also growing. but as a programmer, do you fall into an OO form, PO thinking?
What about programming traps?
As a programmer in the POP age, to calculate the price of all the doors of a house, they will write such a function.
Code:
/*************************************** ***
Function: priceOfHouseDoor ($ num, $ price)
Function: calculates the total price of all doors.
Parameter: $ num number of doors, $ price of each door
Note: surfchen @ http://www.yubeinet.com/
**************************************** **/
Function priceHouseDoor ($ num, $ price)
{
Return $ num * $ price;
}
Echo priceHouseDoor (2, 5); // output price
Or let's just make a more process:
Code:
$ Doors = 2; // number of doors
$ Price = 5; // The price of each door
Echo $ doors * $ price; // output price
This is a typical POP. after learning about OOP, many people may change the code that implements this function. maybe, programmers will write this:
Code:
Class house
{
/*************************************** ***

Function: getDoorPrice ($ num, $ price)
Function: calculates the total price of all doors.
Parameter: $ num number of doors, $ price of each door
Note: surfchen @ http://www.yubeinet.com/
**************************************** **/
Function getDoorPrice ($ num, $ price)
{
Return $ num * $ price;
}
}
$ House = new house;
Echo $ house-> getDoorPrice (2, 5); // output price
As we can see, this code uses the representatives of OOP-classes 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. my understanding is: Object-oriented programming has two core contents. class 1 and instance 2. class is the abstraction of a class of things.
For example, 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
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.
Understandable... as we know, OOP is actually a microcosm of our real world. therefore, the house in OOP has no connection with the door of the house and it is not understandable.
Dimension.
So how should we use real OO to implement the above functions? See the following code:
Code:
Class house
{
Var $ doors; // number of doors
Var $ pricePerDoors; // price of each door
Function setNumOfDoors ($ num) {$ this-> doors = $ num;} // set the number of doors
Function setPricePerDoor ($ price) {$ this-> pricePerDoors = $ price;} // set the price of each door
/*************
* Obtain the door Price *
*************/
Function getDoorPrice ()
{
Return ($ this-> doors) * ($ this-> pricePerDoors );
}
}
$ House = new house;
$ House-> setNumOfDoors (2); // set the number of doors
$ House-> setPricePerDoor (5); // set the price for each door
Echo $ house-> getDoorPrice (); // output
We can clearly see that the number of doors and price houses of the above code have been combined. this is the real object-oriented.
Don't you think? Pai_^

I haven't published a technical article for a long time. today, I saw a brother posting this post in 21PHP and turned it over. I used -------------------- to strengthen OOP learning ----------------------...

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.