473,396 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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?
Jul 16 '05 #1
1 2046
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

Jul 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: pablo | last post by:
Dear NewsGroupers, I am relatively new to OOP and cannet get my head around this problem. I have two classes. Class Child extends Parent. There is no constructor for the Child class. So when I...
8
by: CJack | last post by:
hy, I have an mdi application, i create a child form and I want to know when a button is pressed while that child form is loaded. I have this code: private void frmTestBaby_KeyUp(object sender,...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
3
by: John Dalberg | last post by:
I am setting the HttpContext.Current.User in the Application_AuthenticateRequest event in global.asax.cs. When I use the IsInRole function in a web page, it works fine. So far so good. (Note that...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
3
by: Maximiliano | last post by:
Hello, I have an asp.net project that calculates a general tax. Ok, this tax is a big object formed with another child objects (as a mather of fact 15 another child object within it), like Ship,...
2
by: Karthik Gurusamy | last post by:
Hi, Wondering if there is a way to measure a child process's cpu usage (sys and user) when the child is still running. I see os.times() working fine in my system (Linux 2.6.9-42.7.ELsmp), but it...
3
by: SteveKlett | last post by:
I'm using a on-demand, SaS ERp application. I would like to customize a few aspects of the application and I can as they have the ability to "deploy" custom scripts with the served up pages. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.