Avoid the form of OOP, the idea of pop
Long time no technical article, today in 21PHP saw a brother sent this post, turned over. Learning to enhance OOP is a bit of a
----------------------------------------------------------
Avoid the form of OOP, the idea of pop
With the enhancement of OOP (object-oriented programming) support and the development of programming ideas, more and more PHP programmers are entering the world of OOP. With simple learning, most programmers can
Get a quick grasp of the basic skills of OOP programming. There are more and more strings in the code written by programmers, such as class,new. But as programmers you, whether you fall into an OO form, po thought
What about programming traps?
As a pop-age programmer, in order to calculate the price of all the doors of a house, they would write such a function.
Code:
/******************************************
Function: Priceofhousedoor ($num, $price)
Function: Calculates the total price of all doors
Parameters: Number of $num doors, $price price per door
Remark: surfchen @ http://www.yubeinet.com/
******************************************/
function Pricehousedoor ($num, $price)
{
return $num * $price;
}
echo Pricehousedoor (2,5);//Output price
Or a more procedural one:
Code:
$doors Number of =2;//doors
$price =5;//the price per door
Echo $doors * $price;//Output price
This is a typical pop. When you learn about OOP, many people may change the code that implements that functionality. Perhaps programmers would write:
Code:
Class House
{
/******************************************
Function: Getdoorprice ($num, $price)
Function: Calculates the total price of all doors
Parameters: Number of $num doors, $price price per door
Remark: surfchen @ http://www.yubeinet.com/
******************************************/
function Getdoorprice ($num, $price)
{
return $num * $price;
}
}
$house =new House;
echo $house->getdoorprice (2,5);//Output price
We can see it all at once, and this code uses the representation of OOP-class and instantiation. So, is this the object-oriented programming?
I look at it again and find out that this class is actually just the object-oriented form of the Priceofhousedoor ($num, $price) function above.
Well, now let's talk about what it's called object-oriented programming. My understanding is that object-oriented programming has two core content. One is the class, the other is the instance. A class is an abstraction of a kind of thing, and a real
An example is an individual in a class of things. The attributes (values) and methods (functions) of an instance are the specific contents 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 doors of the houses, one is the price of the house door. We
It is found that when we use this method, these two values are built on a temporary basis and do not have any connection with the House. In real life, a house door with no contact with the house, it is not
Understandable ... As we know, OOP is actually a microcosm of our real world. So, there is no connection between the house and the door of the house in OOP, and it is incomprehensible and non-oo thinking.
- dimensional.
So, how do we use real oo to achieve the above features? See the following code:
Code:
Class House
{
var $doors;//Number of Doors
var $pricePerDoors;//The Price per door
function Setnumofdoors ($num) {$this->doors= $num;} Set Number of Doors
function Setpriceperdoor ($price) {$this->priceperdoors= $price;} Set the price per door
/*************
* Get the price of the door *
*************/
function Getdoorprice ()
{
Return ($this->doors) * ($this->priceperdoors);
}
}
$house =new House;
$house->setnumofdoors (2);//Set gate number
$house->setpriceperdoor (5);//Set the price per door
echo $house->getdoorprice ();//Output
It is clear to us that the number of gates and price of the above code has been combined with the house. This is the real object-oriented.
Don't you think? ^_^
http://www.bkjia.com/PHPjc/532013.html www.bkjia.com true http://www.bkjia.com/PHPjc/532013.html techarticle avoid the form of oop, pop thought for a long time did not send technical articles, today in 21PHP saw a brother sent this post, turned over. Learning to enhance OOP is a bit of a----------------------...