Skip to main content
Participant
April 16, 2023
Answered

Error on Components package and ACCESS="PACKAGE" in function

  • April 16, 2023
  • 2 replies
  • 642 views

Hi all,

 

I test the use of access="package" in a cfc function and it doesn't work, although I do exactly what the documentation says. It says that

1) cfcs in the same folder are considered a package and

2) a func having access="package" can be accessed by the functions of the all the other components of the same package (folder).

 

Please see the simple code below. All 3 files are in the same folder.

When I call  index.cfm  I get the message "Variable CFC2FUNC is undefined"

 

<!--- index.cfm --->

<cfobject name="oX" component="cfc1">
<cfset x = oX.cfc1func()>
<cfoutput>#x#</cfoutput>

 

<!--- cfc1.cfc --->

<cfcomponent>
<cffunction name="cfc1func" access="public" returntype="string">
    <cfset local.ret = cfc2func()>
    <cfreturn Ret>
</cffunction>
</cfcomponent>

 

<!--- cfc2.cfc --->

<cfcomponent>
<cffunction name="cfc2func" access="package" returntype="string">
    <cfreturn "ABC">
</cffunction>
 </cfcomponent>

 

What is not correct in the code above?

 

Thanks in advance for your help

Kostas

 

This topic has been closed for replies.
Correct answer Charlie Arehart

You've misconstrued how things work there, but you're very close. You still need to create an instance of cfc2 (in cfc1) in order to call a method in it from within cfc1 (or from within any cfml template).

 

To be clear, the access control doesn't change HOW you call methods, only WHO (what template) can call it, once the cfc is instantiated. With public, any can; with private, no other can; with package, others can but only if in the same package/folder; while remote is like public but the method can be called from outside of cfml. 

 

See  https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-f/cffunction.html

2 replies

BKBK
Community Expert
Community Expert
April 17, 2023

Charlie has given you one suggestion for a solution. Namely, to create an instance of cfc2, and use it to make the cfc2func() call. Pretty much as you've done in index.cfm.

 

A second suggestion is to use the current code, but with cfc1 extending cfc2. That is:

 

<!--- cfc1.cfc --->
<cfcomponent extends="cfc2">
<cffunction name="cfc1func" access="public" returntype="string">
    <cfset local.ret = cfc2func()>
    <cfreturn Ret>
</cffunction>
</cfcomponent>

 

However, you shouldn't do that just for convenience. It is poor design to have ArbitraryComponent.cfc to extend AnotherArbitraryComponent.cfc.

 

There should be a reason in your design for using extension. Examples:

  • Dog.cfc extends Animal.cfc;
  • Employee.cfc extends Person.cfc;

 

 

Participant
April 22, 2023

BKBK

thank you for your reply. Your comment is alsolutely right. Extending whatever from wherever to whatever else wherever it is, is poor design and in case of middle and up size projects it leads to a mess "OOP".

 

Kostas

 

Charlie Arehart
Community Expert
Charlie ArehartCommunity ExpertCorrect answer
Community Expert
April 16, 2023

You've misconstrued how things work there, but you're very close. You still need to create an instance of cfc2 (in cfc1) in order to call a method in it from within cfc1 (or from within any cfml template).

 

To be clear, the access control doesn't change HOW you call methods, only WHO (what template) can call it, once the cfc is instantiated. With public, any can; with private, no other can; with package, others can but only if in the same package/folder; while remote is like public but the method can be called from outside of cfml. 

 

See  https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-f/cffunction.html

/Charlie (troubleshooter, carehart. org)
Charlie Arehart
Community Expert
Community Expert
April 16, 2023

In my first writing of this, I mistakenly typed public in the 2nd paragraph where I clearly meant package, while distinguishing the 4 types. I've since corrected that. 

/Charlie (troubleshooter, carehart. org)