so, would you add a function to return all if you wish?  I am finally
breaking my habits with php and trying the oo approach also.  So, this:
function Customer($customer_id = 0)

doesn't always set the customer_id to 0 even when you pass one in?

Eddie

-----Original Message-----
From: Chris W. Parker [mailto:[EMAIL PROTECTED]
Sent: Monday, April 19, 2004 1:33 PM
To: [EMAIL PROTECTED]
Subject: [PHP] oo question


hi.

i've recently realized that the little "oo" code i've written is
actually not very oo at all. it's more like procedural code wrapped in a
class.

armed with my new knowledge i'm in the process of modifying my current
classes to be more oo (or what i like to this is more oo). so i'm going
to ask two questions and show two different examples stripped down to
their bare minimums as to not flood the list with code.

1. my question has to do with abstraction (i think that's the right
word).

CLASS AS IT STANDS CURRENTLY:

class Customer
{
        function get_customer_info($customer_id)
        {
                // grab data from db

                // return data in form of array
        }
}

USAGE:
<?php

        $customer_id = 45;

        $cust = new Customer;

        $customer_data = $cust->get_customer_info($customer_id);

        echo "first name: {$customer_data[0]['fname']}\n";
        echo "last name: {$customer_data[0]['lname']}\n";
        echo "age: {$customer_data[0]['age']}\n";
?>

PROPOSED CHANGE:

class Customer
{
        var $fname;
        var $lname;
        var $age;

        function Customer($customer_id = 0)
        {
                if($customer_id > 0)
                {
                        $this->initialize_customer($customer_id);
                }
        }

        function initialize_customer($customer_id)
        {
                // grab data from db

                $this->fname = $...[0]['fname'];
                $this->lname = $...[0]['lname'];
                $this->age   = $...[0]['age'];
        }

        function first_name($fname = "")
        {
                if(empty($fname))
                {
                        return $this->fname;
                }
                else
                {
                        $this->fname = $fname;
                }
        }

        function last_name(...)
        {
                // same as above but with last name
        }

        function age(...)
        {
                // same as above but with age
        }
}


<?php

        $customer_id = 45;

        $cust = new Customer($customer_id);

        echo "first name: ".$cust->first_name()."\n";
        echo "last name: ".$cust->last_name()."\n";
        echo "age name: ".$cust->age()."\n";
?>


so although the second class has a lot more code in it, it also allows
me to change what happens behinds the scenes (i.e. variable names) more
easily. for example the customer will always have a "first_name" but i
may not always want to use $fname to represent it within the class.

this revised class, in my limited experience, seems to be much more oo
than my current class.

seeing as how this email turned out longer than i had planned i will
only be asking one question at this time. in fact i can't even remember
what my second question was anyway. :)


any and all comments are appreciated!


thanks,
chris.

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

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

Reply via email to