Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scope in CFC

Status
Not open for further replies.

ice78991

Programmer
Nov 20, 2006
216


Question 1:


Within a <cffunction> there are 2 declarations

<cfset var someVar = 1>

and

<cfset someVar1 = 50>


Is it correct to say that someVar is only acccessible within the same <cffunction> whereas someVar1 will be accessible within all <cffunctions> contained in the cfc AND also accessible in any cfcs that extend this cfc

Question 2:

I have also seen cfcs that begin with the line

<cfset init()>

where init() will return the handle of a struct created with structNew()

For example

<cfset instance = structNew()>

<cfreturn instance>

What would the scope of instance be here ? Accessible within all cffunctions of this cfc and any cfcs that extend this cfc ( ie the same effect as <cfset someVar1 = 50> ) ?
 
Is it correct to say that someVar is only acccessible within the same <cffunction>

Yes, because its declared with the keyword "var"

whereas someVar1 will be accessible within all <cffunctions> contained in the cfc AND also accessible in any cfcs that extend this cfc

Conceptually, yes. "someVar1" was declared without a scope, so by default its placed in the VARIABLES scope. The VARIABLES scope is accessible to other "cfcs that extend this cfc".


What would the scope of instance be here ?
...
(ie the same effect as <cfset someVar1 = 50>) ?

Yes. Because "instance" is placed in the VARIABLES scope.


where init() will return the handle of a struct created with structNew()

Its not required, but usually init() methods return a reference to the CFC instance. That way the function acts like a default constructor in a java class.

Code:
<cffunction name="init" ...>
   <cfreturn this />
</cffunction>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top