Hello all. I am at the point in my PHP learning that I am trying to understand how classes work. I am building an online RPG game using PHP and I would like to put classes to work for me. I know there is probably a better or easier way to do what I'm trying to do without using a class, but I want to try to incorporate classes into my project that I can understand them thtough practical example.
I want to build an oop class for 'character'
when users log in, they have the option of creating a character to play in the game. Now I have 2 MySQL database tables that will relate to this, 'character', and 'race'
to use 1 single field from each for this example:
In table: 'character' I have a field named 'strength' (one of the character statistics)
In the table 'race' I have a field named 'str_mod' (strength modifier)
I want to create a class to facilitate the character creation process.
I have a question.
- Can I pull external data (as in a database query) to use in the class constructor to initialize the strength stat? For example, I pull the 'str_mod' data from the 'race' table and put it into an array in the standard way so it becomes race[str_mod]. The equasion to initialize the strength stat is (10 + (race[str_mod])).
so I would like to have something like this:
(in php 4)
<?php
class char {
var strength;
function char() {
$this->strength = (10 + (race[str_mod]));
$sql = "INSERT INTO `Character` ( `strength`)
VALUES ('$this->strength');
$result = $db->sql_query($sql);
}
}
?>
would that work (assuming external data)?