I'll give you the typical example that just about everyone uses, the database connection.
Lets look at a typical PHP script that interacts with a mysql database: (stolen from http://www.php.net/manual/en/ref.mysql.php)
<?php
/* Connecting, selecting database */
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die("Could not connect : " . mysql_error());
echo "Connected successfully";
mysql_select_db("my_database") or die("Could not select database");
/* Performing SQL query */
$query = "SELECT * FROM my_table";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
/* Printing results in HTML */
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
/* Free resultset */
mysql_free_result($result);
/* Closing connection */
mysql_close($link);
?>
Ok, so this script does the basics of connecting to mysql, selecting the database to use, selecting data from a table and displaying it to the screen. This code could/would be used over and over on any web site for various actions. Now one of the uses of classes is to 'encapsulate' all of the database interation into a class, this hides all of the gory details of getting data into your page and lets you focus on just writing the code for the page. Here is the same actions using a database class.
<?php
/* Connecting, selecting database */
$dbClass = new DataBaseClass("mysql_host", "mysql_user", "mysql_password", "my_database");
if ($dbClass->Error)
{
print $dbClass->ErrorString;
die();
}
/* Performing SQL query */
$query = "SELECT * FROM my_table";
$dbClass->doQuery($query);
if ($dbClass->Error)
{
print $dbClass->ErrorString;
die();
}
/* Printing results in HTML */
echo "<table>\n";
while ($line = $dbClass->fetch_assoc() {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
/* Free resultset */
/* Closing connection */
$dbClass->close();
?>
As you can see the code here is much cleaner than the code above. Everything you are doing is passed through the $dbClass object and you don't really have to worry about how it's doing it's job. Which makes things very nice when you decide that you need to use PostgreSQL instead of MySQL, now instead of re-writing all of your pages to use the new database you just have to re-write/replace DataBaseClass.
Now I know you'll say, but I can do all of that with functions. And yes you can, but what if you had to connect to 2 databases at the same time and keep both of those connections open, it'd get kinda ugly in functions but with classes you already have that ability.
<?php
$db1 = new DataBaseClass("host", "user", "pass", "db1");
$db2 = new DataBaseClass("host", "user", "pass", "db2");
$db1->doQuery("Select username, address from table1");
while ($line = $db1->fetch_assoc() {
$query = "insert into table1 values ('" . $line["username"] . "','" . $line["address"] . "')";
$db2->doQuery($query);
}
?>
As you can see here I just created 2 objects of type DataBaseClass and connected them to 2 different databases and copied the data from a table in the first database to a table in the second, and I didn't have to modify my database code at all since each object holds a connection to a different database. This would be much more confusing using just functions and passing the different databse connections in and out of them since you'd be calling the same function everytime.
There are other benefits that will become available when PHP5 get's released, like Exception's, which make classes even more useful. Exceptions will allow you to re-write my first class example to look like this:
try {
/* Connecting, selecting database */
$dbClass = new DataBaseClass("mysql_host", "mysql_user", "mysql_password", "my_database");
/* Performing SQL query */
$query = "SELECT * FROM my_table";
$dbClass->doQuery($query);
/* Printing results in HTML */
echo "<table>\n";
while ($line = $dbClass->fetch_assoc() {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
} catch (DBError dbe) {
print $dbe->ErrorString;
} finally {
/* Free resultset */
/* Closing connection */
$dbClass->close();
}
?>
This moves all of your error handling into a single location, making your code even cleaner and easier to read and understand.
I hope this simple explanation helps you. Let me know if you have any questions.