tags:

views:

80

answers:

2
$starcraft = array(  
    "drone" => array(   "cost" => "6_0-", 
                        "gas" => "192",
                        "minerals" => "33",
                        "attack" => "123",

                    )
    "zealot" => array(  "cost" => "5_0-", 
                        "gas" => "112",
                        "minerals" => "21",
                        "attack" => "321",
                    )               
)

I'm playing with oop and I want to display the information in this array using a class, but I don't know how to construct the class to display it.

This is what I have so far, and I don't know where to go from here. Am I supposed to use setters and getters?

class gamesInfo($game) {
    $unitname;
    $cost;
    $gas;
    $minerals;
    $attack;
}
+5  A: 

You're actually pretty close so far.

In OOP, each object usually represents a discrete concept. Therefore, a better name for your class would be Unit because it represents a unit in the game. Example:

class Unit {
    var $name;
    var $cost;
    var $gas;
    var $minerals;
    var $attack;

    function setName($name) {
        $this->name = $name;
    }
    function getName() {
        return $this->name;
    }
}

Note that this example has the appropriate instance variables (name, cost, etc) and a getter/setter pair for the name variable. You'd want to add more getter/setter pairs for the other instance variables.

Once you have all your getter/setters, you can instantiate a Unit by doing this:

$zealot = new Unit();
$zealot->setName("Zealot");
$zealot->setAttack(321);
... etc.

You'll also want to learn about constructors, so that you can instantiate a Unit this way:

$zealot = new Unit("Zealot");
$zealot->setAttack(321);

You can see that a constructor would give you a bit of a shortcut there by letting you set the unit name at the same time you instantiate the Unit class.

So to print a Unit object, you'd do something like:

echo $zealot->getName();

Edit: Like zerkms said, OOP is complex, and what I described here is basically programming with classes and objects. This is just the very beginning of OOP.

Jeff
<snob_mode>i may be wrong, but answers to such kind of questions encourages further asking here questions that can be (and actually should be) simply answered by reading manual</snob_mode> ;-)
zerkms
Much appreciated Jeff :)
Doug
A: 

Why did not you start with http://ru.php.net/manual/en/language.oop5.basic.php?

zerkms