Home  >  Article  >  Backend Development  >  Quickly get started with object-oriented php with CrossFire_PHP Tutorial

Quickly get started with object-oriented php with CrossFire_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:20:18854browse

Copy the code The code is as follows:

/*
* Use CrossFire to quickly get started with php object-oriented !
*php has now become the preferred development language for domestic mainstream web developers. Its powerful object-oriented approach can easily make beginners lose their minds and become confused!
*, with the help of this article, I hope it will be helpful to beginners. To help, assume that Crossfire is developed in php.
* 1 class and object
*/
class Crossfire
{
public $Category='1,2,3'; //Firearms Type, 3 selected weapon categories
public $Weapon='ak|awp|m4a1';//Purchased weapons, weapons owned
public $Flashlight=true;//Whether to purchase*
public $ Bomb=false; //You cannot buy grenades again under the same conditions after purchasing flash
function attack()
{
//Attack code $this->Weapon; Use weapon
}
function repair()
{
//Code to repair the weapon
}
function destroy()
{
//Code to automatically destroy the weapon if it is broken
}
/*
* We regard CF as a class. CF contains main weapons, secondary weapons, firearms, knives, ammunition, body armor, spray patterns, etc..
* The above are all classes. Member attributes in, if the weapon is broken, then we have to repair it, maintenance is the member method of the class!
* Each firearm has different power, different number of bullets, and different sizes, but they are all weapon types, so It is the relationship between classes and objects (the instantiation result of the class is the object).
*
*/
function __construct()
{
//Weapon repair$this->repair();
}
function __destruct()
{
//Weapons automatically disappear when their durability exceeds $this->destroy();
}
private function buy()
{
//Buy weapon private attribute code
}
private function __get()
{
//Call the purchased weapon code
}
private function __set()
{
//Purchase the number of bullets for the purchased weapon Code
}
private function __isset()
{
//Check whether the amount of money is enough to buy the number of bullets
}
private function __unset()
{
/ /If you die midway, the weapon will automatically drop
}
}
class role extends Crossfire
{
//The personal character inherits the attributes of cf, and the character can use to purchase and destroy the above items
function attack()
{
Crossfire::attack();
$this->Weapon='awm-a';
//If the gun is changed midway, add the bullet for changing the gun Amount, damage!
}
final function vip()
{
//Purchase VIP, the final keyword prohibits method and property inheritance! VIP requires money to purchase, so it cannot be inherited!
}
public static $awm=100;//awm damage is fixed, so use static
public static function awm()
{
self::$awm;//awm attack Code, self calls static. Overload of method
}
public function __toString()
{
return $this->repair(); //Automatically return the cost of weapon repair
}
}
interface map
{
//Map code, a subclass can only inherit the parent class once, the map is used by every player, obviously inheritance is not enough The interface solves the bottleneck for us!
public function map1()
{
//Black Town
}
function map2()
{
//Transport Ship
}
}
interface vipmap extends map
{
//If you are a member, display the member map and inherit the interface general map
function _vipmap()
{
//The vip map color is red first
}
function _vipmap1()
{
}
}
$cf=new Crossfire();
echo $cf-> ;Weapon;//Output the owned weapons!
/*
* $Object name = new class name ();
To create a new class we use new, we create a new cf class, $cb is Crossfire objects!
&nb
sp; Polymorphic applications, abstract classes are not mentioned. Of course, a complete game cannot be simple from now on, just a metaphor!

*/
$cf2=clone $cf; //Clone operation means all players use this class!
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325139.htmlTechArticleCopy the code as follows: ?php /* *Quickly get started with PHP object-oriented with CrossFire! *php has now become The preferred development language for domestic mainstream web developers, its powerful object-oriented and easy...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn