Skip to main content
Inspiring
April 2, 2008
Question

Nesting Objects

  • April 2, 2008
  • 1 reply
  • 263 views
I am looking for advice on how to nest objected created from cfcs...

The place I have seen this sdone is in the fusebox framwork in the following:
#myFusebox.getCurrentCircuit().getAlias()#

What I would like to do is create an object called called users from a users.cfc with methods such as listUsers() and findUser(userID)...

Ex. #users.findUser(34)#

Then from there I want to next another object (object might not be the correct term) or set of functions that deal with the user specified such as getName() and getEmail()...

Ex. #users.findUser(34).getName()#
Ex. #users.findUser(34).getEmail()#

Can someone explain to me how this can be done? Thanks!
This topic has been closed for replies.

1 reply

Inspiring
April 2, 2008
jeby wrote:

> Can someone explain to me how this can be done? Thanks!
>

How this is done depends largely on how the objects are related to each
other. I.E. Does one object extend (inherit) the other object creating
an is-a or parent child relationship. Or does one object contain an
instance of the other as a property|variable (composite) creting an
has-a relationship.

I'm doing some web service with complex object testing and I have just
written this simple testing code. See if it makes some sense to you.

basic.cfc
---------
<cfcomponent>
<cfproperty name="foo" type="string">
<cfproperty name="bar" type="string">

<cfscript>
this.foo = "George";
variables.bar = "Gracie";
</cfscript>

<cffunction name="getBar" access="remote" returntype="string">
<cfreturn variables.bar>
</cffunction>
</cfcomponent>

complex.cfc
-----------
<cfcomponent>
<cfproperty name="anObj" type="basic">

<cfscript>
variables.anObj = createObject("component","basic");
</cfscript>

<cffunction name="getObj" access="remote" returntype="basic">
<cfreturn variables.anObj>
</cffunction>
</cfcomponent>

index.cfm
---------
<cfscript>
complexComp = createObject("component","complex");
</cfscript>

<cfdump var="#basicComp#" expand="no">

<dl>
<dt>complexComp.getObj()</dt>
<dd><cfdump var="#complexComp.getObj()#"></dd>
<dt>complexComp.getObj().foo</dt><
dd>#complexComp.getObj().foo#</dd>
<dt>complexComp.getObj().getBar()</dt>
<dd>#complexComp.getObj().getBar()#</dd>
</dl>