Ec (2); preface: the object oriented is profound and profound. People who have never been in contact with each other will feel confused. I learned a lot of materials, but most of them are abstract. So I used the classic game-Starcraft to discuss PHP object-oriented. Now let's assume that we are using PHP to develop Starcraft, so as to get in touch with PHP object-oriented. Note: In order to facilitate learning, except for special instructions, other codes are not associated. In addition, different codes are often used for the same thing. In addition, I am not going to check the attribute numbers of each type of troops. It is just used to explain. & Nbsp script ec (2); script
Preface
Object-orientedProfound and profound. People who have never been in touch will feel confused.
LearningThere are a lot of materials, but most of them are abstract, so I use the classic game-Starcraft for discussion.PHPObject-oriented.
Now let's assume that we are using PHP to develop Starcraft, so as to get in touch with PHP object-oriented.
Note: For ease of learning, except for special instructionsCodeThere is no association between them. In addition, different codes are often used for the same thing.
In addition, I am not going to check the attribute numbers of each type of troops. It is just used to explain.
I. Classes and objects
If a player creates a gunshots, how can we express it? Because each Gunshots have several basic data records: The remaining blood, the number of enemies, and attack power.
We can use an array to record the remaining blood and number of target troops, because it is independent of each other.
However, the attack power is troublesome because the attack power will increase after the upgrade, which requires you to find all the arrays indicating the hosts and modify them.
From this we can see that one thing, first, each machine guard has independent data that needs to be recorded and modified, such as the remaining blood. At the same time, they need to share the same data, such as attack power.
At this time, object-oriented can help us.
1.1. Class Definition
Let's first deal with a part of the problem, that is, the data exclusive to each camera.
Class marine
{
Public $ blood = 50; // remaining blood
Public $ kills = 0; // number of target users
// This function (usually calledMethod) Indicates the Running code when attacking the enemy.
Function attack ($ enemy)
{
// Attack the enemy's code
}
}
This is called a class. We have created a class marine that represents all the gunshots, which retains the data unique to each of them, such as the remaining blood in the code above.
1.2 create and use objects
Next, we will use the object, that is, each gunshots:
$ M1 = new marine ();
With the name and parentheses of A Class added after new, we have created a machine gun $ m1, and $ m1 is called a marine-like object. We can think of it as a special variable, it only saves multiple data.
If you need to use or operate the blood of a certain machine rifle (Object attribute), you only need to use $ m1-> blood to represent it:
Echo $ m1-> blood; // output remaining blood of the gunshots $ m1
Let's create another gunshots.
$ M2 = new marine ();
If $ m1 is attacked by the enemy at this time, there will be 10 blood remaining. $ M2 was not attacked:
Echo $ m1-> blood; // The result is 10.
Echo $ m2-> blood; // The result is 50.
The object can be used to easily store the blood of each gunshots without affecting each other.
If the attacker $ m1 attacks the enemy, you can use the object method as follows:
$ M1-> attack ($ z1); // assume that the target of a puppy is attacked. $ z1
Different classes can use functions of the same name. For example, the Zergling class of a puppy can also have a function attack.
It should be noted that, starting from PHP5, an object's attributes can be changed wherever it is changed. For example, the above puppy object is passed into the attack function as a parameter. After the function is executed, the blood of the puppy object is reduced, which is different from the general function. But this is very intuitive. If a puppy is attacked, its blood should be reduced.
2. constructor and destructor
Each time we create a new gunshots, the total population should be increased by 1. If a gunshots are killed, the population should be reduced by 1.
The constructor and destructor can be used for automatic processing:
</