Professional Web Applications Themes

child object on demand? - PHP Development

I have an application that suffers from my amateur coding inefficiencies, and I am trying to clean a few things up. I have an extremely database intensive object, which I have tried breaking into smaller child objects to reduce database queries when they aren't needed. I currently have something that looks a bit like this: class Object { function Object() { $this->attribute1 = dbQuery("foo"); $this->attribute2 = "bar"; $this->child = new SomeOtherObject($attribute1); } } $foo = new Object(); if ($foo->attribute1 == "X") echo $foo->child->attributeA; Because every page load does not require $foo->child to be populated, is there a way to only ...

  1. #1

    Default child object on demand?

    I have an application that suffers from my amateur coding
    inefficiencies, and I am trying to clean a few things up. I have an
    extremely database intensive object, which I have tried breaking into
    smaller child objects to reduce database queries when they aren't
    needed.

    I currently have something that looks a bit like this:

    class Object {
    function Object() {
    $this->attribute1 = dbQuery("foo");
    $this->attribute2 = "bar";
    $this->child = new SomeOtherObject($attribute1);
    }
    }

    $foo = new Object();
    if ($foo->attribute1 == "X") echo $foo->child->attributeA;

    Because every page load does not require $foo->child to be populated,
    is there a way to only establish the child object if it is referenced?
    Where is a good place to read about dealing with child objects?
    Mike S Guest

  2. #2

    Default Re: child object on demand?

    Mike S wrote:
    > I have an application that suffers from my amateur coding
    > inefficiencies, and I am trying to clean a few things up. I have an
    > extremely database intensive object, which I have tried breaking into
    > smaller child objects to reduce database queries when they aren't
    > needed.
    >
    > I currently have something that looks a bit like this:
    >
    > class Object {
    > function Object() {
    > $this->attribute1 = dbQuery("foo");
    > $this->attribute2 = "bar";
    > $this->child = new SomeOtherObject($attribute1);
    > }
    > }
    >
    > $foo = new Object();
    > if ($foo->attribute1 == "X") echo $foo->child->attributeA;
    >
    > Because every page load does not require $foo->child to be populated,
    > is there a way to only establish the child object if it is referenced?
    > Where is a good place to read about dealing with child objects?
    This is something known as lazy instanciation, if I remember well
    (please someone correct me if I'm wrong...).

    One simple way to do it is :

    class Obj {
    var $_child; /* private */

    function Obj() {
    $this->_child = null;
    }

    function getChild() {
    if ($this->_child == null) {
    $this->_child = new SomeChildObject($whatever);
    }
    return $this->_child;
    }
    }

    $foo = new Obj();
    if ($someCondition) {
    $bar = &$foo->getChild();
    echo $bar->someAttribute;
    }

    If you know in advance which attributes of _child you may want to
    access, you can even make Obj a facade for _child :

    class Obj {
    var $_child; /* private */

    function Obj() {
    $this->_child = null;
    }

    /* private */
    function _getChild() {
    if ($this->_child == null) {
    $this->_child = new SomeChildObject($whatever);
    }
    return $this->_child;
    }

    function getSomeAttributeX() {
    $child = &$this->_getChild();
    return $child->someAttributeX;
    }

    function getSomeAttributeY() {
    $child = &$this->_getChild();
    return $child->someAttributeY;
    }
    }

    $foo = new Obj();
    if ($someCondition) {
    echo = $foo->getSomeAttributeX();
    }

    BTW, note that directly accessing an object's attribute may be a Bad
    Thing(tm) in oop - if the implementation change, all client code is
    broken. So do this only if you're API is really stable, or if you have
    complete control over client code and are prepared to eventually
    refactor all this code (more simply put : avoid doing so unless you
    really know you can do it).

    HTH
    Bruno

    Bruno Desthuilliers Guest

Similar Threads

  1. Parent/Child relations - Trying to access child control for save
    By tnt_lu@hotmail.com in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: April 15th, 12:50 PM
  2. Placeholder child of child control event problem.
    By caldera in forum ASP.NET Building Controls
    Replies: 1
    Last Post: May 28th, 07:56 AM
  3. child object oddity
    By Word of Mouth Productions in forum Macromedia Director Lingo
    Replies: 2
    Last Post: October 22nd, 11:54 PM
  4. demand dialed ppp
    By Ralph Blach in forum Linux Setup, Configuration & Administration
    Replies: 2
    Last Post: September 1st, 05:39 PM
  5. problems with adding Camera as child to object
    By Bazmati webforumsuser@macromedia.com in forum Macromedia Director 3D
    Replies: 1
    Last Post: August 14th, 05:29 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139