From Procedural to OO. What to turn into classes?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
mhoney
Forum Newbie
Posts: 7
Joined: Tue Sep 02, 2008 5:36 pm

From Procedural to OO. What to turn into classes?

Post by mhoney »

I have a very simple website that lets users submit custom cd labels and in turn lets others download the labels. I am just getting started in the OO world of programming and am having trouble getting into the right mind set. I understand the theory (I think) but am having trouble converting the procedural code I have into OO. For example, I have a class called "labels" that defines the attributes of a label and has the methods that allow for getting and setting those attributes. I also have a database class for interacting with my Mysql database that stores the labels. What I am failing to understand is how to glue these classes together. When someone goes to my site (shameless plug http://www.customcdlabels.com) and the index.php page is called, it calls a page called newlabels.php that queries the database for labels submitted within the last 10 days and creates a list of links to these labels. Is index.php supposed to be some sort of master controller class/object or is it supposed to remain a normal php script? Would my newlabels.php script be considered an object or would it simply be a method in the labels class that queries the database and returns the newest labels? It is relatively easy to identify an object when you can assign attributes to it but I am getting confused with how abstract I should go with objects and when I should decide that I just have a method and not an object but no particullarly good place to put this method. Any pointers are greatly appreciated.

Marc
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: From Procedural to OO. What to turn into classes?

Post by allspiritseve »

Here are some buzzwords to google: "MVC" and "Front Controller".

As a really basic start, maybe make a class that has a method for each page (so the code that was previously in newlabels.php would become a method). Here's a quick example:

Code: Select all

class LabelsController  {
 
    public function __construct ($db)   {
        $this->db = $db;
        $this->gateway = new LabelsGateway ($db);
    }
 
    public function new()   {
        $labels = $this->gateway->fetchLatest (10);
        $template = new Template ('new_labels.tpl');
        return $template->render();
    }
 
}
Note: I've labeled this a Controller, but it is really a Controller+View in MVC terms.

Here's a really simple template (I keep all my HTML out of classes, which means templates use a little PHP. Worthy trade-off, in my opinion).

Code: Select all

<?php foreach ($labels as $label): ?>
<h1><?php echo $label->title; ?></h1>
<img src="<?php echo $label->image; ?>" />
<?php endforeach; ?>
Here's a simple Template class:

Code: Select all

class Template  {
 
    public function __construct ($template) {
        $this->template = $template;
    }
    
    function set ($key, $value) {
        $this->data[$key] = $value;
    }
    
    function render()   {
        extract ($this->data);
        ob_start();
        include ($this->template);
        return ob_get_clean();
    }
    
}
Finally, in index.php just call the appropriate method on LabelsController for whatever page you want. It may help to start out with an array of keywords that map to classes, like this:

Code: Select all

$routes = array (
    'key' => 'ClassName',
    'labels' => 'LabelsController'
);
If you're using clean URLS (highly recommended), you could do something like this:

http://www.customcdlabels.com/labels/ calls method index() on class LabelsController
http://www.customcdlabels.com/labels/new/ calls method new() on class LabelsController
http://www.customcdlabels.com/labels/popular/ calls method popular() on class LabelsController
etc...
mhoney
Forum Newbie
Posts: 7
Joined: Tue Sep 02, 2008 5:36 pm

Re: From Procedural to OO. What to turn into classes?

Post by mhoney »

Thank you for the hints and sample code. I have read a little about Smarty and Codeigniter and I like the idea, I will do some more investigating.

Marc
Post Reply