not getting OO

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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  

Re: not getting OO

Quoted text here. Click to load it

Look at the thread "Classes / Functions / Autonomy"

Re: not getting OO

Dikkie Dik wrote:
Quoted text here. Click to load it

excellent link, thanks a lot.
takes some time to read, not because of the length, but because i am
still very new at "thinking" in objects instead of " do this, then do
this, then do this,.... etc"
i appreciate it a lot.


Re: not getting OO

nephish wrote:
Quoted text here. Click to load it

Because you'll have to take care of connecting, updating, reporting errors
etc. That's the job of another class/object.

Rik Wasmus

|Flag-FAQ| Re: not getting OO

Kimmo Laine wrote:
Quoted text here. Click to load it

Unless next_page.php generates PHP, the script with this include will
only get HTML.

Quoted text here. Click to load it


    if (isset($_GET['foo'])) {
      echo '<?php echo $_GET[\'foo\']; ?>';
    } else {
      echo '<?php echo \'Not available\'; ?>';

File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

Re: not getting OO

nephish wrote:
Quoted text here. Click to load it

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.

Re: not getting OO

Jerry Stuckle wrote:
Quoted text here. Click to load it

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.  

Site Timeline