|  | Posted by nephish on 08/17/06 13:00 
Jerry Stuckle wrote:> nephish wrote:
 > > Hey there all.
 > >
 > > i have been looking to simplify my huge website that i wrote while
 > > learning php.
 > > now its a spaghetti mess. So, i wanted to simplify it.
 > > Now, i see the functionality that defining functions can give me. But
 > > what is the magic behind classes?
 > > i mean, the documentation i find. Most use the class Person as the
 > > example. So how would having a person class help me. It seems that i am
 > > writing more code than less because i am passing values thru a class
 > > instead of directly updating a database.
 > >
 > > for example: if i have a class Customer
 > >
 > > $john = new Customer;
 > >
 > > $john->name = 'john';
 > >
 > > then the class updates the database with a new customer named john.
 > >
 > > why not just send sql an insert to do this ?
 > >
 > > just not getting it, but i know the advantages are there, could someone
 > > help me.
 > >
 > > thanks much
 > > shawn
 > >
 >
 > Hi, Shawn,
 >
 > Yes, you do end up writing more code the first time.  However, one of
 > the advantages is you only do it once.  You can then use that class
 > multiple times and not have to rewrite that code each time.
 >
 > It also allows you to forget about the internals of the object and
 > concentrate more on the problem at hand.
 >
 > For instance - if I want to display a list of customers in Washington,
 > DC, (from a database), I could do something like:
 >
 >    $db = new Database();  // config file stores user, pw, db, etc.
 >    $clist = new CustomerList(db);   // Create a new list object
 >    $clist->fetch(array('city'=>'Washington', 'state'=>'DC'));
 >    $list = $clist->getArray();  // Fetch the list as an array of Customer
 >    foreach ($list as $customer) {
 >      echo "<tr><td>" . $customer->getName() . "</td>\n";
 >      echo "<td>" . $customer->getAddress() . "</td>\n";
 >      ... etc.
 >    }
 >
 > I don't' know how the database connects - in fact I don't even know
 > which database I'm using, nor do I care.
 >
 > I don't have to worry about the SQL required to fetch a list of
 > customers.  I just know my function takes an array of key=>value pairs;
 > the key is attribute I'm looking for (documented in the class itself)
 > and the value is the actual value I want.
 >
 > And I don't even know what the internal representation of the Customer
 > is (although it's probably pretty obvious just from the data).  But the
 > important thing is I don't have to worry about it.
 >
 > So, instead of having to worry about how to access the database, fetch
 > my data, possibly reformat the data (is it stored as "name" or
 > "first_name/last_name"?) or any of the rest.
 >
 > It also means I can change databases or even use a flat file just by
 > changing the classes involved. The rest of the program doesn't care.
 >
 > Hope this helps some.
 >
 > --
 > ==================
 > Remove the "x" from my email address
 > Jerry Stuckle
 > JDS Computer Training Corp.
 > jstucklex@attglobal.net
 > ==================
 
 yep, that helps, i can see certainly that in the future having to
 extend what i am writing now, so this is definatly the way to go.
 Thanks a lot for your post.
 -sk
  Navigation: [Reply to this message] |