1 Reply Latest reply on Apr 2, 2008 10:19 AM by Newsgroup_User

    Nesting Objects

    josheby Level 1
      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!
        • 1. Re: Nesting Objects
          Level 7
          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>