On Sun, 25 Nov 2001, Rudi Ahlers wrote:
> Can anyone explain classes to me please? On many sites have I seen classes,

There are people earning PHD's while explaining classes.  I know your
question originates from using PHP, and this is a PHP general mailing list
... but your question is just a tad too general for this list.

> I'm not sure how these work, or how to use them. I'm still learning about

Classes aren't unique to PHP.  You need to learn a true object-oriented
programming language (C++, Java, ... etc.) to really learn classes.
Truly gifted individuals can learn object-oriented programming w/o too
much help, but they'd first have a firm grip on programming.  I'd expect
the average, novice programmer to need a good amount of help learning &
understanding objecte-oriented programming ...  like that attained from a
University, a good High School, or a lot of independent study and time
experimenting with code.

That said ...

You weren't completely missing the boat with your analogy of a class to a
variable, but in the same breath, that idea is totally missing the boat (I
can see from where you're coming, and to where you're headed).  Classes
are an IDEA.  They're not actually anything.  They're the definition of
private and public data members and methods that should make up an object.
When a class is instantiated, an object is created with the defined data
members and methods available.  You can then use the objects' methods to
set, get, alter, or otherwise interact with its data members, or to simply
perform a set of related operations.  <Insert a couple semesters of theory
here.> That's my feeble attempt to explain classes.  It's abstract, I
know, and possibly not a help at all.  But it's because of the paragraph
above this one.

Let's look at some petty code:

class chair{
        // DATA
        var num_legs;
        var color;
        var num_arms;

        // METHODS
        function chair( $legs = 3, $arms = 0 ){ //CONSTRUCTOR
                $this->num_legs = $legs;
                $this->num_arms = $arms;
        }

        function setLegs( $legs ){
                $this->num_legs = $legs;
                return;
        }
        function getLegs( ){
                return $this->num_legs;
        }

        // ... *clip* ...
}


Above is the [incomplete] definition of a chair class.  As you can see,
the class is useless.  But you can instantiate the class, and create a
useable object ... you can now create a chair.

$myChair = new chair( 4, 2 );

Now I have a chair [object] with 4 legs and 2 arms, called $myChair.
Let's have the chair tell us how many legs it has ...

$numLegsOnMyChair = $myChair->getLegs();
print( "My Chair has $numLegsOnMyChair legs." );

Lets change how many legs the chair has ...

$myChair->setLegs( 7 ); // very odd, seven-legged chair

We should have a chair with 7 legs now, instead of 4.  Prove it ...

$numLegsOnMyChair = $myChair->getLegs();
print( "My Chair has $numLegsOnMyChair legs." );

(As you alluded to previously, the object $myChair is seemingly a variable
that has access to scripts ... but I hope you see now that $myChair is an
object with methods and data members, not a variable.)

That's very simple, and not too useful.  Which brings me to the next
point.  Knowing object-oriented programming is to know when and when not
to use it (especially with PHP).  I don't need a class definition and an
object for a one-time use 7-legged chair.  But I may need a class
definition if I were making many complete graph data structures, each with
a number of nodes equal to a unique number in the Fibonacci sequence.  I
wouldn't want to re-code the basic logic of a complete graph over and over
for each graph.  I could just instantiate graph objects, each with
different numbers of nodes, much like I did with the chair, above.
</ramble>

        g.luck,
        ~Chris                           /"\
                                         \ /     September 11, 2001
                                          X      We Are All New Yorkers
                                         / \     rm -rf /bin/laden


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to