The actual web pages don't need to be OOP. But think about this.
Lets say you've got two pages - one that lists all customers in a detailed grid, and one that just lists their names. The customers are stored in a MySQL database.
In CustomerDetails.php, you probably have:
Code:
$q = mysql_query("SELECT * FROM Customers");
$rows = mysql_fetch_array($q);
foreach ($row in $rows)
{
echo "Name: " + $row["Name"];
echo "Email: " + $row["Email"];
}
And LittleCustomerList.php has this:
Code:
$q = mysql_query("SELECT * FROM Customers");
$rows = mysql_fetch_array($q);
foreach ($row in $rows)
{
echo "Name: " + $row["Name"];
}
Notice they do the same thing? That's code duplication. What if someone changes the table "Customers" to "tblCustomers"? You have to go to two files to rename it. Not good (this is an extremely simple, but common, example). What if "Name" changes to "FirstName"/"LastName"? Theres a tone of code that will have to change, and this makes it much harder to maintain.
So, you can go OOP by doing this (please, forgive me, the syntax might be wrong, but I haven't used PHP for years):
Code:
class Customer{
var $Email;
var $FullName;
function GetCustomers()
{
$results = // new array
$q = mysql_query("SELECT * FROM Customers");
$rows = mysql_fetch_array($q);
foreach ($row in $rows)
{
$cust = new Customer();
$cust.Fullname = $row["Name"];
$cust.Email = $row["Email"];
$results.Add($cust);
}
return $results;
}
}
Then, your first page becomes:
Code:
foreach ($customer in Customer->GetCustomers())
{
echo "Name: " + $customer->FullName;
echo "Email: " + $customer->Email;
}
If the table structure changes so that names are stored in two columns ("FirstName"/"LastName" rather than "Name"), you only change your class - nothing else.
The only thing that knows how to read and write to the customers table is the customer class. This abstraction is what makes OOP great, and and is how you make your applications scale better.
This is the reason I don't use PHP - PHP is so simple, 99% of the users (yeah, that's a generalization, but shutup) just go around embedding database queries and whatever they like wherever they want. They don't think through design properly. ASP.NET, on the contrast, makes it so much easier to follow a proper object oriented design (not that everyone does, but I think it's more likely).
Edit: Another thing to think about. What if someday you decide to support two databases - Access and MySQL? Imagine the hundreds of lines of code you'd have to write to do that the first way. Now imagine how much easier it would be doing it the OOP way. Just change the Customers class, and done. OOP makes life good