- 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.
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.
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: