StarCraft PHP Object-oriented (ii)

Source: Internet
Author: User

Preface
==============================================================================
This PHP design mode album from the blog (jymoz.com), now has no access, this series of articles I have been looking for a long time to find the complete, thanks to the author Jymoz's Hard Pay Oh!
==============================================================================

V. Access control

If you use $attacknumber = 10 to indicate a property, the system defaults to public $attackNumber = 10, so it is recommended to write:

<? PHP      class Marine    {        publicstatic$attackNumber// attack number     }? >

Public indicates that this property is common, and that it can be accessed and manipulated anywhere.
However, there are some problems, if a player knows some code structure of class Marine, then he makes a simple patch, run the time to load up:

<? PHP     // Patches    Marine::$attackNumber = 10000;? >

In this case, his machine-gun soldiers have 10000 of the attack, hehe, so, who beat him!

To do this, we use private, which means that this property is accessible only to functions within the class:

<? PHP      class Marine    {        privatestatic$attackNumber//attack number//         This function indicates the running code of the machine-gun upgrade         function  upgrade ()        {            // This prevents unlimited upgrade            if (self::$attacknum<13)            {                self::$attacknum++            ;        }    }} ?>

This way, only upgrades can change the attack power of the gunner.
But now is often the team development, and many use the inheritance of the class, if private, the subclass can not be accessed, but do not want to be able to modify some of the properties.
Then you can use the Protected,protected property to access the function of the quilt class.

Six, overload 6.1, property overloading

If we take the ground troops as a class, let the machine-gun class to inherit him, if the ground troops class and the machine gun class all define the attack $attacknumber, then each soldier's attack is decided by the machine gun class, not the ground force. This is called overloading.

<?PHP//ground Troops    classGroundarmy { Public $attackNumber= 5; }    //machine Gun    classMarineextendsGroundarmy { Public $attackNumber= 10;//number of attack damage    }    $m 1=NewMarine ();//Create a new machine gun .    Echo $m 1->attacknumber;//show attack damage is ten?>

6.2. Function overloading

Overloads can also be used for functions where the subclass's function has the same name as the parent function, and unless otherwise noted, the object of the subclass calls the function within the subclass by default.
For example, the Terran Ghost and the Dark Church of God (Hidden dao), are stealth classes, but the ghost will be invisible to reduce energy, the dark Templar has no energy properties.
If we take stealth as a parent, the Ghost class Ghost and the Dark Church of God's class darktemplar to inherit it, while implementing different stealth codes:

<?PHP//Stealth Capability Class    classconcealability {//This function represents the invisible running code        functionconceal () {//Stealth Code of Operation        }    }    //Ghost Soldier Class    classGhostextendsconcealability {$energy= 150; //This function represents the invisible running code        functionconceal () {//Invisible Run code//reduce the energy of the Ghost, $this represents the current object, that is, the current ghost soldier            $this->energy-= 25; }    }    //Dark Sagrada Familia class    classDarktemplarextendsconcealability {//This function represents the invisible running code        functionconceal () {//invisible running code that does not affect energy        }    }    //Create a new ghost soldier    $g 1=NewGhost (); //Show Energy as    Echo $g 1-Energy ; //Stealth of Ghost soldiers    $g 1-conceal (); //Show Energy as a    Echo $g 1-Energy ; //Create a new dark chapel    $d 1=Newdarktemplar (); //Dark Sanctuary Invisible, he has no energy attribute    $g 1-conceal ();?>
Seven, interface

PHP does not allow multiple inheritance, so some of the problems are tricky.
If in order to standardize processing, we put the invisible ability to build a class, and then put the ability to fly a class, then the Terran reconnaissance aircraft how to deal with? Cannot inherit two classes!
Well, we don't have to inherit, but the other people in the development group, once it comes to reconnaissance planes, do you want to read the long code again? Is it possible to know a brief description of all methods of a class?
Interface interface can be used, a class can execute (inherit) multiple interfaces, the function defined in the interface cannot have a function body, and the class that executes the interface must define the functions completely.
In this way, we know that the reconnaissance aircraft realizes the flight capability interface, which must have the flight method described in the interface://Stealth Capability interface

<?PHPInterfaceconcealability { Public functionconceal (); }    //interface for flight capability    Interfaceflyability { Public functionFly (); }    //reconnaissance aircraft class    classWraithImplementsFlyability,concealability {//This function represents the operating code of a reconnaissance aircraft flight.        functionFly () {//run code for flight        }        //This function represents the stealth operation code of the reconnaissance aircraft.        functionconceal () {//Stealth Code of Operation        }    }?>
Viii. Summary

We discuss the basic knowledge of PHP object-oriented, through the classic game of StarCraft, we can see the preliminary role of object-oriented.
We see that the code can be made clearer by object-oriented, and the class organizes the code and makes it easier to reuse.
The object also reduces the conflict of variables, and facilitates the preservation and use of related data.
If the problem to be solved involves many aspects, object-oriented can evolve into more flexible and skillful ways, such as the design patterns commonly mentioned, and many frameworks.
Of course, object-oriented also has shortcomings, from the above code can be seen, the first code is more, simple task if the definition of many classes, instead of trouble.
For simple tasks, object-oriented can also make the code less efficient to run.
In-depth discussion, beyond the scope of this article.

StarCraft PHP Object-oriented (ii)

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.