Beginner tip: cfc's and init function
I was thinking about this over the weekend. If you just started with coldfusion or been reading around about coldfusion and cfc's you've probably read that it's a good idea to have an init function in every cfc that you make. This is a good idea to do for a number of reasons, but I haven't read to many explanations on why to call it init. Init seems to be an arbitrary designation with any reason. It's not. However, it's hard to look at a cfc and see why it would need to be called init and why you would even need one in the first place. the simple answer is: that's how coldfusion interacts with java. however, for a more detailed answer, read on for the explanation:
First off init comes from coldfusion's integration with java. with java classes (and classes in other types of languages) there is what's called a constructor. This is a fancy term for a function that initializes the class (similar to a cfc in coldfusion) and sets up any hierarchy and initial terms. the constructor call in most languages is an automatic function that gets called when your class is created. like for example, in a java class (let's look at a bit of java code):
private String Message = "";
public void Hello() {
Message = "Hello World!";
}
public static void sayHello() {
System.out.println(Message);
}
}
The constructor is the function that has the same name as the class. in order to call this function, you'd usually call it with some code that looks like this:
helloClass.sayHello;
what that does is it creates a class (like a cfc) and automatically calls the constructor Hello function. what this does is set the string. Now this is a simple example, constructors are often used for much more than simply setting a string value, but the principle is the same.. constructor does stuff to initialize the class.
ColdFusion doesn't have these automatic constructors. for ColdFusion, you have to explicitly call a constructor function. As you've read previously, most people suggest that you call this constructor init and that you call it with every cfc creation call. for example, like this:
what this does is that it sets up your cfc with initial values. So after all that, we get back to the original question.. why init? why not setup? why not new? why not create? why not any number of other names? well, the reason is that this is how coldfusion interacts with java. when you create a java class, in order to call the constructor on that java class, you use the init() call.
From the coldfuson docs:
Although the cfobject tag loads the class, it does not create an instance object. Only static methods and fields are accessible immediately after the call to cfobject.If you call a public non-static method on the object without first calling the init method, there ColdFusion makes an implicit call to the default constructor.
To call an object constructor explicitly, use the special ColdFusion init method with the appropriate arguments after you use the cfobject tag; for example:
<cfobject type="Java" class="MyClass" name="myObj">Note: The init method is not a method of the object, but a ColdFusion identifier that calls the new function on the class constructor. So, if a Java object has an init method, a name conflict exists and you cannot call the object's init method.
<cfset ret=myObj.init(arg1, arg2)>To have persistent access to an object, you must use the init function, because it returns a reference to an instance of the object, and cfobject does not.
You can see the full coldfusion doc here
So that's the reason for calling the arbitrary constructor init. What that means is that if you use the init() function, your java code and your coldfusion code will look the same and will be easier to read.
There are no comments for this entry.
[Add Comment]