Using Interstellar QuickStart PHP object-oriented Programming (Revised edition)

Source: Internet
Author: User

Objective

Object-oriented and profound, for never contact with people, will feel confused.

Learning A lot of information, but mostly more abstract, so I use the classic game-StarCraft to discuss the object-oriented PHP .

Now let's assume that we're using PHP to develop StarCraft to touch the object-oriented PHP.

Note that, in order to facilitate learning, there is no association between parts of the code except for special instructions. And the same thing often uses a different code.

In addition, I do not have to verify the attributes of each class number, only to explain.

I. Classes and objects

If the player creates a machine-gun, then how do we represent him, because each of the riflemen has a few basic data to record: The remaining blood, the number of enemies, attack and so on.

We can use an array to record the amount of blood and the number of kills that a machine-gun has left, because it is independent of each of the machine-gun soldiers.

But the attack is more troublesome, because after the upgrade, the attack will increase, this must find all the expression of the gun soldiers, and then modify the array, very troublesome.

One thing we can see from here is that each of the riflemen has separate data to record and modify, such as the rest of the blood. At the same time they have the same data need to be shared, such as attack.

At this point the object-oriented can help us.

1.1, the definition of the class

Let's deal with some of the problems, the unique data of each machine gun.

Class Marine

{

Public $blood = m; The rest of the blood

Public $kills = 0; Number of Enemies

This function (usually called a method ) represents the running code when attacking an enemy
function Attack ($enemy)

{

Code to attack the enemy

}

}

This is called class, and we've created a class marine that represents all the machine-gun soldiers, which retains the data that is unique to each soldier, such as the rest of the blood in the code above.

1.2, the creation and use of objects

Next we're going to use the object, which is each of the riflemen:

$m 1 = new Marine ();

By adding the name and parentheses of a class after new, we create a new $m1, $m 1 is called Class Marine object, we can think of it as a special variable, but there are many data stored inside.

If you need to use or manipulate the blood of a gun soldier (the object's properties), just use $m1->blood to indicate:

Echo $m 1->blood; The output of the machine-gun soldier $m1 the remaining blood

We're going to build a gun soldier.

$m 2 = new Marine ();

If $M1 is attacked by the enemy at this time, there are 10 blood left. And $m2 was not attacked:

Echo $m 1->blood; The result is 10.

Echo $m 2->blood; The result is 50.

The use of objects can be very simple to save the blood of each machine-gun soldier, does not affect each other.

If the riflemen $m1 attack the enemy, you can use the object method in this way:

$m 1->attack ($z 1); Let's say it's an object of a puppy $Z1

Different classes can use a function of the same name, such as a puppy's class zergling inside also can have a function attack

It should be noted that starting from PHP5, no matter where you change the attributes of an object, you can change it. For example, a puppy object is used as a parameter to pass into the attack function of a machine-gun, and the blood of this puppy object is reduced after executing the function, which is different from the normal function. But it is intuitive that if a puppy is attacked, its blood should be reduced.

Ii. Constructors and destructors

Every time we create a new machine-gun, the total population should be 1, and if a machine-gun is killed, the population should be reduced by 1.

Can be handled automatically through constructors and destructors:

</

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.