SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question help on patterns

    Hi,

    I have a problem with patterns. I've got Customer class that contain details of a customer and a webcustomer class that extends the customer class. The webcustomer class contain special data like username, password, etc...

    Usually I use handler for all my class like CustomerHandler class that have get, insert, create, delete, ... method.

    How can I construct an handler that can handle my base class and my specialized class in the same time. Is it a good idea?

    Thanks for all,

    David

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Rome, Italy
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends on how your data is mapped on objects and on how your handlers act upon these objects.

    You could use php get_class() function to determine the class of your objects (customer or webcustomer) and than choose the right handler (using 2 handlers in this case) or use the same function in insert(), update() and delete() methods of your only handler to determine the right SQL to execute.

    Ciao.
    Last edited by mfila; Aug 30, 2004 at 05:21.

  3. #3
    SitePoint Member
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mfila
    It depends on how your data is mapped on objects and on how your handlers act upon these objects.

    You could use php get_class() function to determine the class of your objects (customer or webcustomer) and than choose the right handler (using 2 handlers in this case) or use the same function in insert(), update() and delete() methods of your only handler to determine the right SQL to execute.

    Ciao.
    Here is is construct. I think it's better to create a second handler for my WebCustomer class, what do you think?

    class Customer extends Object {
    function Customer($id = null)
    {
    $this->initVar('cid', OBJ_DTYPE_INT, null, false);
    $this->initVar('gender', OBJ_DTYPE_TXTBOX, null, false, 1);
    ...
    }

    function cid()
    {
    return $this->getVar("cid");
    }

    function gender($format="S")
    {
    return $this->getVar("gender", $format);
    }
    }

    class WebCustomer extends Customer {
    function uname($format="S")
    {
    return $this->getVar("uname", $format);
    }
    }

    class CustomerHandler extends ObjectHandler {
    function &create($isNew = true)
    {
    $customer = new Customer();
    if ($isNew) {
    $customer->setNew();
    }
    return $customer;
    }

    function &get($id)
    {
    if (intval($id) > 0) {
    $sql = 'SELECT
    *
    FROM
    '.$this->db->prefix('customers').'
    WHERE
    cid='.$id;
    if (!$result = $this->db->query($sql)) {
    return false;
    }
    $numrows = $this->db->getRowsNum($result);
    if ($numrows == 1) {
    $customer = new Customer();
    $customer->_assignVars($this->db->fetchArray($result));
    return $customer;
    }
    }
    return false;
    }
    ...
    }

  4. #4
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The most important thing is to avoid duplicating a lot of code. Looks to me like the handlers will be almost identical. You could make the two handlers and then eliminate the duplication afterwards.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Rome, Italy
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by puff_daweed
    Here is is construct. I think it's better to create a second handler for my WebCustomer class, what do you think?
    You could be right. Anyway the best solution is, as dagfinn suggests, to write 2 handlers and then refactor them if needed.

    Ciao.
    Last edited by mfila; Aug 30, 2004 at 08:33.

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sure that I've read up on a pattern that remove the need for 2 seperate handlers, though for the life of me I cannot remember which one it was

    I'll see what I can find, and get back if I have any luck.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •