Jump to content

OOP Related Question


makeshift_theory

Recommended Posts

Ok, I've recently got addicted to the OOP part of PHP, and I have a question reguarding to the inheirtance featured in PHP.  From what I've read you can only extend to one class however if you chain them all of the properties from a ancestor class should be present in the child class.  For example:
[code]
Class example1
{
    public var $home = "new home";
    function __constructor($text)
    {
        $this->home = "$test";
    }
}
Class example2 extends example1
{
    public var $ex2 = "ex2";
}
Class example3 extends example2
{
    public var $ex3 = "ex3";
}
Class example4 extends example3
{
    function print_stuff()
    {
      echo $this->home;
      echo $this->ex2;
      echo $this->ex3;
      return true;
    }
}
$ex = new example("This is a test");
$ex4 = new example4;
$ex4->print_stuff();
Result
------------------
"This is a test" "ex2" "ex3"
[/code]

Is this about right?  If so then I can ask my next question, however I want to make sure my understanding is correct first, thanks.
Link to comment
Share on other sites

Ok well with that being said, here is the code I have to inquire about:
[code]
class stats extends login
{
/*
Gathers all stats relevant to character
*/
var $int; // Intelligence
var $str; // Strength
var $agi; // Agility
var $wis; // Wisdom
var $def; // Defense
static $sta; // Stamina
var $hp; // HP
var $mp; // MP
var $clvl; // character level

function gather_stats()
{ // Gathers stats based on the login classes id
$this->update_session();
$this->db_query("SELECT * FROM char_stats where c_id = '$this->chosen_char' and a_id = '$this->id'"); // Get specific character information
$result = $this->db_readall();
list($blank,$blank,$blank,$this->hp,$this->mp,$this->wis,$this->agi,$this->int,$this->str,$this->def,$this->sta,$this->clvl) = $result; // Assign Results
$blank=''; // clear our blank container since list always starts with the 0 iterator
$_SESSION['clvl'] = $this->clvl; // Register Character Level
return true;
}


}
class draw_items extends stats
{ // Draws all needed items to screen.

function draw_login()
{ // Draws Login fields to screen for use
$login = "<form action=" . $_SERVER['PHP_SELF'] . " method='POST'>";
$login .= '<table width="45%" cellspacing="0" cellpadding="0" border="0">';
$login .= '<tr>';
$login .= '<td>';
$login .= 'E-mail: ';
$login .= '</td>';
$login .= '<td>';
$login .= '<input type="text" name="username">';
$login .= '</td>';
$login .= '</tr>';
$login .= '<tr>';
$login .= '<td>';
$login .= 'Password: ';
$login .= '</td>';
$login .= '<td>';
$login .= '<input type="password" name="password">';
$login .= '</td>';
$login .= '</tr>';
$login .= '<tr>';
$login .= '<td align="right" colspan="2">';
$login .= '<input type="submit" value="Login" name="submit">';
$login .= '</td>';
$login .= '</tr>';
$login .= '</form>';
echo $login;
return true;
}
}
class char_select extends draw_items
{ // Handles the character select menu

const total_chars = 3; // Total number of allowed characters
var $players_chars; // Number of players characters that he or she currently has
var $chosen_char; // Stores chosen characters id

function get_char_list()
{ // This will connect to database get a list of all of the person's characters and display them so the player can choose a character to play with
$this->db_query("SELECT * FROM char_table WHERE a_id = '$this->id'"); // This query will find all characters associated with the login account number
$this->players_chars = mysql_num_rows($this->queryr); // Assigns number of players characters
$this->draw_char_menu(); // Draws the character menu
return true;
}
private function draw_char_menu()
{
if($_SERVER['REQUEST_METHOD']=='POST')
{
extract($_POST);
foreach($_POST as $k => $v)
$key_c = $k;
$_SESSION['cid'] = $key_c; // Set Session Character ID
$this->chosen_char = $_SESSION['cid']; // Set character id
include "js/opengame.js"; // Open and Start Game
}
else
{
$char_table = "<form action=". $_SERVER['PHP_SELF'] ." name='charform' method='POST'>";
$char_table .= '<table width="650" cellspacing="0" cellpadding="0" border="0" align="center">';
$char_table .= '<tr>';
while($r = mysql_fetch_array($this->queryr))
{
$char_table .= '<td width="200" valign="top">';
$char_table .= '<table width="200" height="400" cellspacing="0" cellpadding="0" border="1" align="center">';
$char_table .= '<tr>';
$char_table .= '<td height="25" valign="middle" align="center">';
$char_table .= $r[4] . " " . $r[3];
$char_table .= '</td>';
$char_table .= '</tr>';
$char_table .= '<tr>';
$char_table .= '<td align="center" valign="middle">';
if($r[5] == "") // If the picture is null then use default
$r[5] = "images/portraits/default.jpg";
$char_table .= '<img src="'.$r[5].'" border="0" alt="'.$r[3].'\'s Portrait">';
$char_table .= '</td>';
$char_table .= '</tr>';
$char_table .= '<tr>';
$char_table .= '<td height="25" valign="middle" align="center">';
$char_table .= '<input style="width:200px" type="submit" value="Play This Character" name="'.$r[0].'">';
$char_table .= '</td>';
$char_table .= '</tr>';
$char_table .= ' </table>';
$char_table .= '</td>';
}
$char_table .= '</tr>';
$char_table .= '</table>';
$char_table .= "</form>";

echo $char_table;
return true;
}


}
}
class monster extends char_select
{ // Does Monster Related Things

var $mint; // Intelligence
var $mstr; // Strength
var $magi; // Agility
var $mwis; // Wisdom
var $mdef; // Defense
var $msta; // Stamina
var $mhp; // HP
var $mmp; // MP
var $mimg; // M image
var $mlvl; //M lvl
var $mname; // M Name'
var $mdrop; // Monster Drop List
var $malive; // If monster is not dead stay to 1
var $mmid; // Monster ID
var $mmlvl; // For Storeing Monster Level


function get_monster()
{ // Gets a random monster based on character level
$this->update_session(); // Updates Variables
$mid = array(); // init array
echo $this->clvl;
---->$this->mlvl = rand($this->clvl,2); // Produces a random number between characters lvl + 5
$this->db_query("SELECT * FROM mon_table WHERE mon_lvl = '$this->mlvl'"); // Compiles a list of all same lvl monsters

while($r = mysql_fetch_array($this->queryr))
$mid[] = $r[0]; // Compiles a list of all monster Id's

$nummonsters = count($mid);  // nummonsters is equal to the total number of monsters the query located
$randommonster = rand(0,($nummonsters - 1)); // Produces random monster integer

if($this->malive == 1)
{// Checks for refresh cheat, so they can't switch monsters by hitting refresh
$chosen_mon = $this->mmid;
$chosen_mlvl = $this->mmlvl;
}
else
{
$chosen_mon = $mid[$randommonster];
$chosen_mlvl = $this->mlvl;
}


$this->db_query("SELECT * FROM mon_table WHERE mon_lvl = '$chosen_mlvl' and mon_id = '".$chosen_mon."'"); // Will produce the random monster
$results = $this->db_readall();

list($this->mmid,$this->mlvl,$this->mhp,$this->mmp,$this->mstr,$this->mint,$this->magi,$this->msta,$this->mdef,$this->mname,$this->mimg,$this->mdrop) = $results; // Assign variables
$this->malive = 1; // Monster is active NO SWITCHING! and NO REFRESHING WILL SAVE YOU
$_SESSION['malive'] = 1; // Set Session Variable
$_SESSION['mmid'] = $this->mmid;
$_SESSION['mmlvl'] = $this->mlvl;
return true;

}
function draw_monster()
{ // Ouputs the imagegry for monster
$buffer = '<img src="images/monsters/'.$this->mimg.'" alt="'.$this->mname.'" border="0" >';
echo $buffer;

}

}
[/code]

I have declared my classes in the page like so:
$monster = new monster;
$stats = new stats;

My question is when I run my page, I do not get a value for $this->clvl in the monster class.  Although with inheirtance I should.  To fix the problem in the stat class I assign clvl to a Session and then I have a method that updates sessions which is why "$this->update_session(); // Updates Variables"  is the first like in the getmonster method.  Shouldn't I be able to get the value of $this->clvl without have to store it as a session is my question.
Link to comment
Share on other sites

Actually No. I didn't really look at your implimentation.

Setting a property on the fly within the construct of example1 will never show up in example4. youve got to rememebr that when you instantiate an object ($obj = new obj()) it is its own instance.

So, in your example $ex and $ex4 are two completely different objects.

And in fact, when you instantiate a child class, its parents construct is not called automatically. You'd need to call it manually within each child.
Link to comment
Share on other sites

Okay now that makes a lot of sense, so to clarify and correct me if I'm wrong please.  Since I stored monster as a new object seperate from stats, monster has no idea what the new stats are.  In my program I use $stat->get_stats() so all of the stats get updated however since I'm using $monster as a second instance it has no knowledge of the new stats, is this correct?

EDIT: PS> Thanks thorpe for the quick responses, your awesome.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.