Chris,

Apr 19 at 10:33am, Chris W. Parker wrote:
> my question has to do with abstraction (i think that's the word).

I think what you're talking about here is encapsulation. And yes, you are
correct to point out that the second approach allows the more
encapsulation of the object's data. Using methods to set or get the data
stored in the object provides an interface to the object, whereas
accessing the object's properties directly isn't a true interface.

> PROPOSED CHANGE:
> class Customer {
>    ...
>    function initialize_customer($customer_id) {
>        // grab data from db
>        $this->fname = $...[0]['fname'];
>        $this->lname = $...[0]['lname'];
>        $this->age   = $...[0]['age'];    }

This is perfectly fine, but if you want to use those get/set methods as an 
interface, you're better off not setting the properties as above...

How about something like this:
(also took some liberties with the variable/method names...)

class Customer {

    var $res = array();
    var $id, $fname, $lname;

    function Customer($id=null) {
        $id === null || $this->initCustomer($id); }        

    function firstName($fname=null) {
        $fname === null || $this->res['fname'] = $fname;
        return $this->res['fname']; }

    function lastName($lname=null) {
        $lname === null || $this->res['lname'] = $lname;
        return $this->res['lname']; }
    ...
    function initCustomer($id=null) {
        $id === null || $this->id = $id;
        $row = $db->getOne($query); // grab assoc array of db row...
        return $this->res = $row; }

    function exportCustomer() { return $this->res; }

I think that may better illustrate the benefit of the interface. You can
store the data inside the object, but thanks to encapsulation only the
object itself needs to know how to access or manipulate the data that is
stored inside. As a user of the object, you never need to know what the
internals of the object are. As the developer, you don't need to worry if
you want to change the structure, as long as the interface remains intact.

Since you really don't want to go TOO far with the OO in PHP4, you can 
always store the simple scalar data in the object's properties, and just 
use that as a defacto interface.. for instance $obj->id = 35; is not much 
different from $obj->id(35), and $x = $obj->id; is similar to $obj->id();
Decide which you prefer, but encapsulation is an important OO concept.

My point: it's redundant to have $obj->fname and $obj->firstName(),
and may encourage mixing the way you access the object data from your 
code, which could potentially defeat the purpose of the get/set method.

On that note, you may be interested in the __call(), __get() and __set()
magic methods in PHP5, as they will give you the best of both worlds.
...Among many other nice OO feature improvements in PHP5...

-- 
Kelly Hallman
// Ultrafancy

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

Reply via email to