0

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.

3
Contributors
5
Replies
8
Views
6 Years
Discussion Span
Last Post by Heru_1
0

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

Edited by arrgh: n/a

0

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'.

Edited by fiju: n/a

1

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()>

Edited by arrgh: n/a

0

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

0

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#>

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.