Well, the 'Table' class is definitely of limited usefulness..

but where classes come in especially handy is when you want to build
reusable code for handling common tasks, like, for instance, MySQL
connections and queries... so that your PHP code looks something like

/****************************************************************/

$db = new mysqlObject;

$db->connect("localhost","username","password","mydatabase");
$db->doQuery("select * from mytable");
foreach($db->resultRows){
        //Do stuff with the query results here
}
$db->disconnect();

/******************** instead of something like: ****************/

$dbconnection = mysql_connect("localhost","username","password");

mysql_select_db("mydatabase",$dbconnection);
$result = mysql_query("select * from mytable",$dbconnection);
$resultrows = array();
while($row = mysql_fetch_array($result)){
        $resultrows[] = $row;
}
foreach($resultrows as $temprow){
        //Do stuff with the query results here
}
mysql_disconnect($dbconnection);

/****************************************************************/

The first, most obvious advantage is that the code becomes a easier to read,
which is important if you're working on a complex project.

You could arguably create a set of regular functions for MySQL stuff if you
really wanted to, but you'll have to keep careful track of any global
variables you might be using;  When you're working with a class all of your
class variables and functions are properties of an object, always with the
same name and easily accessed, but separate from any local variables you're
working with.  When you're working with a collection of functions, your
variables are all over the place and prone to inconsistency.

For instance, handling two different database connections in the same script
is easy with a class; using the hypothetical example from above, you know
that the results of your query are always going to be stored in the
'resultRows' property of the object, so it's as easy as accessing
$foo->resultRows and $bar->resultRows.

If you were using a collection of functions, you would have to come up with
a second variable name to store results from the second connection for that
particular script, and even if you try to maintain consistency among all the
scripts on your site using your collection of database functions, chances
are that things will get messy at some point; it's easier to keep track of
commonly used variable and function names at the class level.

Hmm, I hope that sort of made sense... it's a tough thing to explain.  It
took me a long time to get my head around the usefulness of OOP, too- as you
said, examples are usually not very practical.  The first explanation of OOP
that I ever read dealt with a hypothetical 'vehicle' class, and although I
understood the *theory*, I wondered for the longest time why I would want to
write a program for describing different kinds of imaginary cars and turning
them on and off, or extend it to describe imaginary boats and motorcycles.
;)

-Andy


> -----Original Message-----
> From: mojo jojo [mailto:[EMAIL PROTECTED]]

> I've been using php for a while now but I have not got my head around OOP
> (classes).
>
> Why bother using them? I've read thru a few tutorials on using classes and
> the examples given are quite simple. This is probably the problem - I just
> can't see the benefit of using this style of programming.

:: table class example snipped ::

> Using a class doesn't appear to give me any benefits - in fact the code is
> longer.
>
> I know that you can spawn more instances of the same class which sounds
> useful, however I can also run my function as many times as I like using
> different variables.
>
> What am I missing here?


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to