Copy link to clipboard
Copied
Hi, I'm still confuse with the use of init() with cfc.
If I only have the init function on my cfc but I don't set anything within the init(), for example:
<cfcomponent> <!--- 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>
<!--- When I 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#>
Copy link to clipboard
Copied
<cfreturn THIS ></cfreturn>
Everything seems all right, except this end tag. You should delete it.
I can think of the following 2 examples to illustrate your theme.
Example 1
CFM
<cfset myObj = createObject("component", "MyComponent").init(5)>
<cfinvoke component="#myObj#" method="ValidateEmpSal" returnvariable="x">
<cfoutput>#x#</cfoutput><br>
<cfinvoke component="#myObj#" method="ValidateAddr" returnvariable="y">
<cfoutput>#y#</cfoutput>
CFC
<cfcomponent>
<CFFUNCTION name="Init" access="public" returntype="any" output="false" hint="Returns an initialized component instance.">
<cfargument name="id">
<cfset empID = arguments.id>
<cfreturn THIS >
</CFFUNCTION>
<!--- Then I have many more functions below --->
<CFFUNCTION name="ValidateEmpId">
<!---- validation codes here --->
</CFFUNCTION>
<CFFUNCTION name="ValidateEmpSal">
<!---- validation codes here --->
<CFRETURN "Salary of employee " & empid & " validated.">
</CFFUNCTION>
<CFFUNCTION name="ValidateAddr">
<!---- validation codes here --->
<CFRETURN "Address of employee " & empid & " validated.">
</CFFUNCTION>
</cfcomponent>
Example 2
CFM
<cfset myObj = createObject("component", "MyComponent").init()>
<cfset variables.employeeID=6>
<cfinvoke component="#myObj#" method="ValidateEmpSal" EmpId="#variables.employeeID#" returnvariable="x">
<cfoutput>#x#</cfoutput><br>
<cfinvoke component="#myObj#" method="ValidateAddr" EmpId="7" returnvariable="y">
<cfoutput>#y#</cfoutput>
CFC
<cfcomponent> <!--- 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.">
<!--- instance variable --->
<cfset empID = 0>
<cfreturn THIS >
</CFFUNCTION>
<!--- Then I have many more functions below --->
<CFFUNCTION name="ValidateEmpId">
<!---- validation codes here --->
</CFFUNCTION>
<CFFUNCTION name="ValidateEmpSal">
<cfargument name="empID">
<!---- validation codes here --->
<CFRETURN "Salary of employee " & empid & " validated.">
</CFFUNCTION>
<CFFUNCTION name="ValidateAddr">
<cfargument name="empID">
<!---- validation codes here --->
<CFRETURN "Address of employee " & empid & " validated.">
</CFFUNCTION>
</cfcomponent>
Copy link to clipboard
Copied
Thank you for the examples. I can understand example 1 clearly but with the second example I still don't get the benefits of calling init() because I can just do the following and get the same result when using cfinvoke, am I right?
<cfset variables.employeeID=6>
<cfinvoke component="component", "MyComponent" method="ValidateEmpSal" EmpId="#variables.employeeID#" returnvariable="x">
I'd more curious to know when does init() and <cfreturn THIS> are needed other than being used to pass a parameter. Is this a dumb question?
in addition, I don't understand when you wrote on top of your previous respond: <cfreturn This> need to be deleted. I thought <cfreturn This> is required and when deleted it will generate error.
In other words, reading this article confuse me: http://objectorientedcoldfusion.org/init-function.html
It is says that I need to create init() function everytime I create a .cfc even though my init() does not do anything.
So that is actually my question, what does init() do? just instantiate an object and do nothing else? assuming I just have this:
<cffunction name="init" output="false">
<cfreturn this>
</cffunction>
So it I have 1000 users who log in to my app and call the same function, my component will be instantiated 1000 times each for these users?
I have not used init() so far and my app run just fine but I want to code properly so that's why I want to learn more. Thank you for helping
Copy link to clipboard
Copied
BYJ_wntrsnt wrote:
Thank you for the examples. I can understand example 1 clearly but with the second example I still don't get the benefits of calling init() because I can just do the following and get the same result when using cfinvoke, am I right?
<cfset variables.employeeID=6>
<cfinvoke component="component", "MyComponent" method="ValidateEmpSal" EmpId="#variables.employeeID#" returnvariable="x">
Yes, you will of course get the same result. However, one advantage of <cfset myObj = createObject("component", "MyComponent").init()> over <cfinvoke component="MyComponent"> is reuse, resulting in more efficiency.
Imagine you had to develop an application that required thousands such invocations. In the first case, the object can be used over and over again. Whereas in the second case, the component instance is used just once, and a new invocation will have to be performed each time.
in addition, I don't understand when you wrote on top of your previous respond: <cfreturn This> need to be deleted. I thought <cfreturn This> is required and when deleted it will generate error.
There is a misunderstanding. I meant you should delete </cfreturn>.