|
Posted by Colin Fine on 10/02/12 11:28
Jace Benson wrote:
> Ok I have read alot of things on zend.com, php.net and other sites went
> to the wikibooks to try to understand how to use a class. I have this
> project I want to do that I am sure would work great with a class. I
> just don't grasp the whole concept, and how to do it.
>
> I want to make a Collectable Card Game Draft Engine...(if any of you
> play VS System, LOTR, Magic: The Gathering, you know what I am talking
> about.)
>
> It would be way to complicated for just typing in regular functions and
> calling em.
>
> So this is what I've seen That I don't understand.
>
> Say i have a database with cards that are common(c), uncommon(u),
> rare(r) from 3 sets alpha(a), beta(b), unlimited(u).
>
> I need to 1st create 24 packs that contain 11commons, 3uncommons, and
> 1rare each.
>
> Bam thats a class.
>
> would it look like this?
> Class CreateDraft{
> var $set;
> var $rarity;
> function SelectRandomCard{
> //connect to database in file header
> //
> $querycardbase = "SELECT * FROM `mtg_base` WHERE rarity = $rarity
> AND set = $set LIMIT $rarity_num";//rarity and rarity_num are related
> c=11,u=3,r=1, set dictates cards available.
> $querycardbase_result6 = @mysql_query ($querycardbase);
> while ($cardbaserow = @mysql_fetch_array ($querycardbase_result)) {
>
> $packarr = $cardbaserow[card_id]
> }
> function InsertPack{
> $insertpack = "SQL STATEMENT INSERTING AN ARRAY";//I know, i know
> its late and I just want to understand.
> }
> }
>
> I guess I am asking for help.. Because I do not understand.
>
I don't understand in detail what you're trying to do, but if you have a
class called 'Create<anything>', the chances are you've not understood
classes.
A class (or rather, an object of a particular class) represents a thing
- in your application probably a pack of cards, maybe an individual
card, maybe a set (alpha, beta), maybe a rarity (though that might be a
single value, in which case there's probably no point in having a class
for it).
Create would then be a method (function belonging to a class) - perhaps
the constructor that every class must have, perhaps a different method.
If you declare a function inside a class as you have for
SelectRandomCard, it is a method, so when it is called it will be called
to operate on a particular object (instance of the class), and you need
to refer to the properties of the particular object with the 'this'
pointer: $this->rarity.
Don't use classes because you think they're cool: use them if you're
prepared to think of your program in terms of creating and operating on
objects (that know how they should be operated on).
I now usually approach any programming question in terms of objects, but
it did take some work to get there!
Colin
[Back to original message]
|