Skip to main content
February 19, 2007
Question

Calling CFC's from with cfc's

  • February 19, 2007
  • 3 replies
  • 714 views
I have 2 CFC's one with site wide functions and one with section functions, I want to reference the site cfc from with the section cfc.

How do I reference the site cfc from within the section cfc.

Cheers
This topic has been closed for replies.

3 replies

Inspiring
March 22, 2007
<cfcomponent name="section" extends="SiteWide" >
BKBK
Community Expert
Community Expert
March 22, 2007
To expand on one of Adam's suggestions: "passing in as an argument to a method", your pseudo-code becomes

site = create component (done is the application file for now)

<!---
assume site.cfc is in the folder structure myWebsite/cfc/ above the web root
--->
registration = create component (in the registration section for now)

Within the registration.cfc
<cffunction name=registration>
<cfargument name="st" required="true" type="myWebSite.cfc.site">
...
<cfscript>
site=Arguments.st;
site.sendemail("address")
<cfscript>
...
</cffunction>


Participant
February 19, 2007
phlebas, I think you're running into an issue here. On your CFM page, you're invoking the CFCs and thinking the two CFCs can communicate with each other. It doesn't work like that unless you either put them into appropriate application/session scope or you invoke the cfc within the cfc.

For example:
session.site = createObject('component','...');
registration = createObject('component','...');

Then, inside registration's cfc, you'd have:
<cfscript>
session.site.sendemail("address")
<cfscript>

Otherwise, you'd have to do this:
<cfcomponent>
<!--- constructor zone --->
<cfset siteObject = createObject('component','...')>
<!--- constructor zone --->
<cffunction name="register">
<cfset siteObject.sendemail("address")>
</cffunction>
</cfcomponent>

Note that siteObject is created between the component tags, but is not created in any of the cffunction tag. That means that siteObject is now available to all functions within that component. Otherwise, if you want it available to just one function, then move the siteObject creation inside that one function.

Hope this helps.
February 19, 2007
3 Years off Coldfusion has made my memory play tricks it seems. Thank you both for your answers.

My "fuzzy" memory tells me there is a standard scope you can put cfc's in to save creating a new object (and system resources) everytime you need to call one.

Inspiring
February 19, 2007
> I have 2 CFC's one with site wide functions and one with section functions, I
> want to reference the site cfc from with the section cfc.
>
> How do I reference the site cfc from within the section cfc.

The same way you reference a CFC in a CFM template.

--
Adam
February 19, 2007
I am creating cfc components. I assumed I could reference the component within a CFC, the code was similar to this:

site = create component (done is the application file for now)

registration = create component (in the registration section for now)

Within the registration.cfc I tried to reference the site CFC like this:
<cffunction name=registration>
...
<cfscript>
site.sendemail("address")
<cfscript>
...
</cffunction>

I get the error message "variable site undefined" which leads me to believe I can not reference a cfc component from with a cfc.

I should have written this in full the first time, sorry.

Cheers