I've been focused on php for about 2 years now from an absolutely non-programming background. I think my procedural php is coming along ok, I have dealt with most typical functions of a php driven site... eg. user authentication, database integration, general security, file handling, emailing, image manipulation, and all those smaller things that make it all work.

But I have this feeling that something better is just around the corner, and at my stage that must be OOP. I don't exactly want to change direction I just want to do the same stuff better.

So I thought bugger it, I might as well take the plunge now and try OOP.

The project in mind is basically a directory website with email marketing laid over the top. Procedurally I can and have previously done similar stuff so the only major challenge is the change in technique.

Here's how I am thinking about approaching the project... allow me to wow you all with my caveman knowledge....

Code Layout:
Usually I have one or more functions files that contain all my common functions. Where a page requires something to happen, like retrieve a GET id or update a database row, I layout what I want to happen in a procedure at the top.
eg.
PHP Code:
if (isset($_GET['action']))
{
  
$action $_GET['action'];
  if (isset(
$_GET['id']))
  {
     
$id = (int)$_GET['id'];
     
     switch (
$action)
     {
        case 
'delete':
        
mysql_query("DELETE FROM table WHERE id = $id");
        break;
        
        
// etc etc
     
}
  } else {
    
$output['error'][] = 'no id';
  }
}
// start html 
The fundamental difference in OOP is that I have an object where the delete action is embedded in the class.
PHP Code:
if (isset($_GET['action']))
{
  
$action $_GET['action'];
  if (isset(
$_GET['id']))
  {
     
$id = (int)$_GET['id'];
     
     
// lets say i'm changing a blog post
     
$object = new post();
     
     switch (
$action)
     {
        case 
'delete':
        
$object->deletePost($id);
        break;
     }
  } else {
    
$output['error'][] = 'no id';
  }
}
// start html 
For readability, I assume its worthwhile leaving some procedural stuff as I have done above, but importantly the main action is happening somewhere else.

I am aware the goal is to completely separate code from content but I don't quite understand this in full.

So for my directory system, some potential classes might be...
- user
- listing
- article
- comment (maybe as part of article)

or basically any tangible element that will have associated behavior (functions).

File Structure:
I will hopefully find that the bulk of my functions.php will now be embedded in classes in classes.php.... this is essentially why OOP rocks because most of the code is all pointing to the same central engine room.

Number of Lines:
The major decrease should be a result of less repetition across various .php pages that do similar tasks. There should be less code embedded in the "webfront" pages and more going on in pure php pages.

Shift in Logic
My current logic is... "it makes sense to build heaps of functions because I often will repeat something and I can just call a function instead of re-writing the procedure."
For example one of the most recent functions was
PHP Code:
function checkRecord($field$value$table)
{
    if (
mysql_result(mysql_query("SELECT COUNT(id) FROM $table WHERE $field = '$value'"),0) > 0)
    {
        return 
true;
    } else {
        return 
false;
    }    

In OOP, when I want to check if a record exists, not much will change except that I will have to work out how to make the function accessible inside a class (if it isn't automatically global). If I wanted to really push my luck I might make a SQL class and incorporate that... Functions are perhaps step one to code centralisation, classes are step 2.

Resources:
- lots of tutorial reading
- Sitepoint's PHP Anthology
- phpclasses.org (more so for study rather than using stuff in production)
- and naturally Sitepoint's OOP forum

For all you guys and girls who already have gone down this road, please add your comments and advice... if any of my above logic is flawed, please point it out so I may ingrain the right thinking while i'm still impressionable