>"Chris W. Parker" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>Torsten Roehr <mailto:[EMAIL PROTECTED]>
    on Monday, April 19, 2004 10:46 AM said:

>> the second approach is definitely much, much better and the right way
>> to go. But what exactly is your QUESTION?

>oh yeah.. umm... i guess at this point it would be "which is better?"...

>as i was writing the email it sort of went in a different direction than
>i had originally planned...

>> One personal suggestion: you could directly put the code from
>> initialize_customer() into the constructor.

>but i'm thinking that i might, at some point, want to use that method
>after the object has been instantiated. i can't think of a specific
>case, but i'm only imagining that it's possible.

OK, though I've not needed this anywhere else in my classes yet. But another
suggestion. You can save a lot of code by generalizing/abstracting the
assignment of your DB result to the class attributes. I'm doing this:

class Base
{
    /**
     * Sets properties based on supplied SQL result array with associative
indices
     *
     * @param array $dbResult
     */
    function initFromResultSet($dbResult = array())
    {
        // if a non-empty SQL result set is supplied
        if  (is_array($dbResult) && count($dbResult) > 0)
            {
                // get class properties as array
                $classProperties =
array_keys(get_class_vars(get_class($this)));

                // array keys become object properties with respective value
                foreach ($dbResult as $key => $value)
                        {
                            // only set values for properties that exist in
the class
                            if  (in_array($key, $classProperties))
                                {
                                    $this->$key = $value;
                                }
                        }
            }
    }
}

Then my 'real' class inherits from Base and calls the initFromResultSet()
method:

class Person extends Base() {

    function Person($personID) {

        // if a valid personID is supplied, get data from DB
        if      (Validate::number($personID, array('min' => 1)))
                {
                    // get person data from DB
                    $query        = 'SELECT * FROM ' . TABLE_PERSONS . '
WHERE personID = ' . $personID;
                    $DB_result    =& $db->query($query);

                       // if person with given ID exists, set attributes
                       if   (is_a($DB_result, 'DB_result') &&
$DB_result->numRows() == 1)
                            {

$this->initFromResultSet($DB_result->fetchRow());
                            }

                    // else set error and show list again
                    else    {
                                // handle error
                            }
                }
    }
}

Of course the DB result must be an associative array with the column names
being equal to your class attributes. This saves a lot of code and can be
used in any class.

Regards,

Torsten

>chris.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to