How to use init function in coldfusion.Could any one explain with a good simpla example.
And also scopes(VARIABLES and THIS) while writing an init function in a component.

init() is just a standard convention most people adopt with components. The init() function initializes any variables needed by the component's functions. Then returns a reference to the current component.

ie

Test.cfm
<cfset myObj = createObject("component", "MyComponent").init(dsn="My DSN")>

MyComponent.cfc
<cfcomponent>
    <cffunction name="init" output="false" returntype="MyComponent">
         <cfargument name="dsn" type="string">
         <!--- initialize variables used within this component --->
         <cfset variables.dsn = arguments.dsn>
         <!--- return a reference to this component --->
         <cfreturn this>
    </cffunction>
</cfcomponent>

And also scopes(VARIABLES and THIS) while writing an init function in a component

The documentation is a better source of information on the VARIABLES and THIS scope. But the main difference is VARIABLES is private. Values stored in that scope are only available to functions w/in the component. The THIS scope is public. Anything you store in THIS can be viewed or changed by the component *and* the calling page. Usually the VARIABLES scope is preferred.

http://livedocs.adobe.com/coldfusion/8/buildingComponents_29.html

Thanks for your response.But , Could you please explain the sentence "return a reference to this component". And also
'<cfreturn this>'. I mean use of 'this'.

return a reference to this component

I just meant return the object you created. That's basically what <cfreturn this> does. When used inside a component, THIS means "the component itself".

<cfcomponent>
<cffunction name="init" output="false" returntype="MyComponent">
  ....
    <!--- Return the component so the calling page can use it --->
     <cfreturn this>
  </cffunction>
</cfcomponent>

The reason you do that is so the calling page can use the component's functions

<!--- "myObj" now contains a MyComponent object --->
<cfset myObj = createObject("component", "MyComponent").init(dsn="My DSN")>

<!--- So you can call any of MyComponent's functions --->
<cfset result = myObj.someFunctionName()>

Thank u very much........ As i was new in Cold fusion , asking this like questions... thanx alot

Hi, thank you for sharing this knowledge. If I'm allowed to ask because I still confuse with the use of init(), what if I only have the init function on my cfc but I don't set anything within the init(), for example:

<cfcomponenent> <!--- For example I have this init() within my component but I don't set anything ---> <CFFUNCTION name="Init" access="public" returntype="any" output="false" hint="Returns an initialized component instance."> <cfreturn THIS ></cfreturn> </CFFUNCTION> <!--- Then I have many more functions below ---> <CFFUNCTION name="ValidateEmpId"> <!---- validation codes here ---> </CFFUNCTION> <CFFUNCTION name="ValidateEmpSal"> <!---- validation codes here ---> </CFFUNCTION> <CFFUNCTION name="ValidateAddr"> <!---- validation codes here ---> <CFRETURN Sal> </CFFUNCTION> </cfcomponent> <!--- WhenI call this init() from my .cfm this way, what will happen?
I don't have anything set within my init(). Is this wrong? ---> <cfset myObj = createObject("component", "MyComponent").init()> <cfinvoke component="#myObj#" method="ValidateEmpSal" EmpId="#empId#> <cfinvoke component="#myObj#" method="ValidateAddr" EmpId="#empId#>

Hi,
Could you please suggest me why super.init( argumentCollection=arguments );
return this;
not call in coldfusion 2021?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.