Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP-OOP] Creating A Player & Bot

Joined
Aug 4, 2010
Messages
572
Reaction score
177
I added comments as you may see below. Basically what you will learn is the default structure of using the term "Inheritance" to make a player and a bot.

Code:
<?php
//Creating a new class which will be our character
class Player
{

//We put everything protected in character so that noone can make their own
protected $hp = 10;
protected $armor = 330;
protected $dmg = 10;

//Using the construct function so that's its immediately executed 
protected function __construct()
{

echo "Character was created <br />";

}

//We don't just want a player and a bot, So we create a Attack function.
public function Attack()
{
echo "We attacked the dragon for " . $this->dmg;

}


}


//The new class that will extend Player so that you can execute and display the protected functions
class Dragon extends Player
{

public function __construct()
{
//!Only Used to display information from the parents construct root, otherwise not needed.
parent::__construct();

}

//Implementing the data for our bot which here is called a "Dragon"
public function Setup($hp, $armor, $dmg)  //Arguments for $this->
{

//In the character class we created the (hp,armor,dmg) we state it again but can be modified after
$this->hp = $hp;
$this->armor = $armor;
$this->dmg = $dmg;

}

}


$Character = new Dragon(); //First line below we can use either "new Dragon || Player
$Character ->Setup(10,300,10); //Inserting our Dragons stats, so were inputting the arugments
$Character ->Attack(); //For our attack function


?>


Inheritance is basically more than 1 class and the first class is called the parent of all the children classes. We can expand data from the parent by simply using the parent key. The keyword extend is what helps us bloat more information into the parents class without actually typing codes inside the class.

----
Hope this helps in any way as a example of something.
 
Last edited:
Skilled Illusionist
Joined
Dec 26, 2010
Messages
390
Reaction score
157
Here's the PHP + Indented Version:)

PHP:
<?php
	//Creating a new class which will be our character
	class Player
	{

		//We put everything protected in character so that noone can make their own
		protected $hp = 10;
		protected $armor = 330;
		protected $dmg = 10;

			//Using the construct function so that's its immediately executed 
			protected function __construct()
			{

				echo "Character was created <br />";

			}

			//We don't just want a player and a bot, So we create a Attack function.
			public function Attack()
			{
				echo "We attacked the dragon for " . $this->dmg;
			}


	}


	//The new class that will extend Player so that you can execute and display the protected functions
	class Dragon extends Player
	{

		public function __construct()
		{
			//!Only Used to display information from the parents construct root, otherwise not needed.
			parent::__construct();

		}

		//Implementing the data for our bot which here is called a "Dragon"
		public function Setup($hp, $armor, $dmg)  //Arguments for $this->
		{

			//In the character class we created the (hp,armor,dmg) we state it again but can be modified after
			$this->hp = $hp;
			$this->armor = $armor;
			$this->dmg = $dmg;

		}

	}


$Character = new Dragon(); //First line below we can use either "new Dragon || Player
$Character ->Setup(10,300,10); //Inserting our Dragons stats, so were inputting the arugments
$Character ->Attack(); //For our attack function


?>
 
Joined
May 23, 2008
Messages
1,071
Reaction score
574
Hm. I have two suggestions:
1) Although personal preference, I would remove most/all unneeded extra newlines.

2) You shouldn't have a NPC class extend from a PC class. There should be a base class Character(or whatever you want to call it), and the PC and NPC classes should both extend from that.
The reasoning for this is because functions may differ quite a bit between a PC and NPC, but a lot of functions will remain somewhat similar, although not exactly the same. You will almost certainly run in to problems between the clashing of NPCs and PCs if you extend NPC from PC. On top of that, creating a base character class and attaching all extendable *C(ex. PC, NPC) classes to it is better for organization and layout.
 
Last edited:
Joined
Aug 4, 2010
Messages
572
Reaction score
177
Let me just explain once more to lessen future conflicts. I combined the Character + NPC class to introduce how "Inheritance" really works. If I was regularly working with multiple class between two different types I wouldn't expand them together.
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Well your code is very tightly coupled.. which defeats the purpose of OOP... And programming in general.

Thus, your tutorial is, quite frankly, neglecting to teach using good programming practices.
 
Back
Top