$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;
}
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
Unitbecause it represents a unit in the game. Example: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
Unitby doing this:You’ll also want to learn about constructors, so that you can instantiate a
Unitthis way: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
Unitclass.So to print a
Unitobject, you’d do something like: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.