On Sun, 25 Nov 2001, Miles Thompson wrote:

> Not such a useless explanation at all, in fact a damned good one. Tight &
> concise, and you immediately related the functions to code examples, along
> with an easy-to-visualize object, the chair.

Sweet ... I'm glad you found it useful!  TY

> 1. Why is a constructor class needed? My OO background is Visual FoxPro,
> and all it requires is a declaration of  the class, it' methods and properties.

It's not actually required.  It's good practice, however.  With the
constructor, a chair effectively defaults to a stool (minimal chair, 3
legs, no arms)  when a chair is created w/o any arguments ($myChair = new
chair;).  It'd be silly to make a chair with no legs[1] or 1 or 2 legs.
In many cases, it'd be silly to create an object without some default
attributes set, or giving the programmer the ability to set attributes at
creation time ($myChair = new chair( 4, 2 ); // a nice chair).  If you
have an object that won't need any initial attention when it's created,
feel free to drop the constructor.  (I don't recommend that ... I just
create an empty one ... out of habbit/practice.)

> 2. Are the chairs properties exposed, or are they private? You use
> functions to both set and retrieve them, which implies that they are
> private.  Even so, PHP's syntax is much cleaner than the tortured syntax
> used in VB.

In PHP the chair's properties are exposed.  There's no sense of public vs.
private data members or methods w/ PHP.  Everything is public[2].  I could
effectively get the chair's number of legs by referencing
$myChair->num_legs.  Using object methods to get and set object data
members is just a matter of good practice too.  I come from a C++
background and developed rigid practices of protecting as much as possible
about objects, and not allowing the programmer (usually myself) muck with
objects' data members, or call its internal methods.  It forces me to
tightly define classes, and make them as useful as possible.  <Insert a
couple semesters of theory here, too :)>

[1] Maybe a chair is inherited from a seat, like a mat or a swing, which
can have no legs :)

[2] This is why I recommended [to the original poster] learning a real OO
language.  There are large differences between classes in PHP and classes
in C++ or Java.  Learning those first would enable a person to understand
PHP classes easily.  Learning PHP classes wouldn't help much in
understanding classes in C++ or Java.  If classes in PHP were to one day
become like C++ classes, everyone who only knew PHP classes would have to
re-learn what a PHP class is.  That's not bad, per se, but is something to

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

> At 03:28 AM 11/25/2001 -0800, you wrote:
> >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]

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