OO advice please

Soldato
Joined
18 Oct 2002
Posts
4,925
Location
Yorkshire
Just after a little bit of advice really.
I’m having a play trying to fully Object Orientate an old Ecommerce site I did a while back, now let’s say I’ve got a Customer class and a Product class, as well as a few others.
When you go to say click on a category link and display all the products for that category usually I’d just have a function that would have the sql statement to get all the products based on the category ID then loop through the results and output a formatted products table.
The way I’m doing it with the objects or how I think it should be done is to really create an array of product objects for that category then just loop through the array and output as before.
Now the question is where should the method be to create this array of products be ? Obviously it can’t be in the products class as it’s not a products responsibility to create copies of itself. I’m thinking it would go in the Customer class as in real life the customer would request the products list. But should it be the customers responsibility to format the list of products in to a readable version or should another class be involved which I use to send the product objects array to, to format the output. Or do I just return the product objects array to the normal php web page and do the formatting there, resulting to php code intermixed.
I know I can do it any of the ways mentioned but which would you say is the most correct?
 
You could just make a method in the Product class that queries the database and returns an array of the products found. Just call this method statically (i.e. Product::getProducts() or something similar) whenever you want the array of product objects, and format and output them on the spot.
 
HTMLHugo : Yeah thats what I usually do in .Net but might go with Inquisitor option , although it would be good to separate the database queries.

What you reccon one database class containing all queries or have seperate ones for each class that requires them ? usualy I just have the one.

Also back to my original question. where should I handle the displaying of the products i.e. formatting / looping through the array of product objects (if I use that method) directly in the class or handle it on the html/php page ?
 
The way I'd do it (in PHP at least) is to have a database abstraction layer such as PEAR::MDB2 to handle database connections, and then just run the queries individually when needed. I've tried encapsulating all database operations in one class before, but it quickly got very, very bloated and was far too rigid.

As for display, I use a templating engine (Smarty) for formatting and output; it allows me to separate business logic and display into two separate layers.
 
Data Abstraction, TableDataGateway (and ActiveRecord if you like) to the rescue!!1!

Code:
<?php

class ProductsData
{
    private $_products = array();
    private $_db;

    public function __construct($db)
    {
        $this->_db = $db;
    }

    private function _fetchCategory($id)
    {
        if (!empty($this->_products)) return;

        $this->_db->prepare('SELECT * FROM `products` WHERE `catId' = :1', $id);
        if ($this->_db->error()) 
        {
            throw new ProductsData_Exception('Error blah blah');
        }
        else
        {
            $this->_products = $this->_db->fetchArray();
        }
    }

    public function getProductsTable($catId = null)
    {
        // lazy load..
        if (!is_null($catId)) $this->_fetchCategory($catId);

        return new TableDataGateway($this->_products);
    }

    public function resetCategory()
    {
        $this->_products = array();
    }
}

?>

Subsitute names blah blah.

Untested blah blah.
 
right you've all given me some ideas to play about with now, was thinking of going the smarty route but then i've got to mess around learning all the smarting coding , ohh well suppose it'll do me good to learn it I supose.

thanks again all
 
Back
Top Bottom