|
Posted by Henk Verhoeven on 10/08/05 01:44
Hi Jace,
If you are used to procedural programming OOP can be hard to grasp in
the beginning. The problem is that you see your computer as a single
entity with a single processor and memory space. Your procedural program
is essentially a list of instructions for this processor, referring to
variables in this memory. You need to let go of that idea. Objects are
more like the those beasts/puppets that run across your screen in a
computer game like boulderdash: a whole bunch of entities that seem to
live inside your computer, each doing its own things, remembering for
itself what happend and what it is doing in its own private memory
space, and interacting with one another.
With OOP, objects are things you can call methods on. If you call a
method on an object, the object may do somthing. It may also remember
somthing. What is does may depend on the things it has previously
remembered. It is like a little home computer: you can type a command,
it will print a reaction (or trigger an error if it does not know the
command). You don't need to know how it works internally. You just need
to know what will happen for each command.
But there's more. Objects are networked. When you call a method on one
object, it may ask other objects to do things by calling more methods on
them. If you call a function in procedural programming, you know what
code will be executed. If you call a method on an object, you don't,
unless you know what object you are calling the method on. But for
making the method call you will probably use a variable in which you
have put a reference to the object. So if at some point your code puts a
different object in that variable, all method calls on that object may
end up executing different code... Yes, object references are much like
function pointers. Those objects can hold other objects in their member
variables and make more method calls on those objects, and so on. This
easily leads to a magnitude of spaghetty you can not even dream of with
procedural code, even if you are using GOTO's! This spaghetty even
changes dynamicly as the program changes the contents of variables! New
objects may be created. References to objects may be returned and stored
.. AARCH!
This is what classes are made for: To create some structure in the
object spaghetty. Firs we classify all objects. Then we only define
methods in the classes. So if you have a variable referencing an object,
and you know the class of the object in advance, you know what code will
be executed if you call a method on that object. Becuase most
programmers somehow tend to know the type (class with subclasses) of
what is in each variable, they can find their way in the spaghetty.
Formally it's still very messy, but who cares, programming IS a matter
of psychology after all ;-)
Greetings,
Henk Verhoeven,
www.phpPeanuts.org.
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.
>
[Back to original message]
|