views:

534

answers:

3

hi all, i have some questions about constructors in ColdFusion :

  1. must i use the name init as the constructor name?
  2. if i create an instance of the component without invoking the init method, what is returned?

    instance=createObject("component","cfcName"); // what value now instance hold

  3. can i take the code in the init method out and delete the init method, then paste the code to the head of the component, is it have a same effect as the init constructor?

great thanks.

+11  A: 

must i use the name init as the constructor name?

No, you can name the initialization function any way you like. init() is merely a convention. And it is no real constructor, since it is not automatically called.

if i create an instance of the component without invoking the init method, what is returned?

The component instance is returned, as you'd expect it. The presence or absence of an init() function is completely irrelevant. There is no notion of static functions in ColdFusion components, you always get a fully constructed instance from GetObject("component", ...). (Not so for Java objects, which are constructed just before first use, if you forgot to/did not do it manually.)

can i take the code in the init method out and delete the init method, then paste the code to the head of the component, is it have a same effect as the init constructor?

Yes, as long as the init() method did not take any parameters, there is no difference.

However, it is a convention to have a method called init() that returns the component instance. Even if it does nothing apart from "<cfreturn this>". I'd stay consistent and add one to every component, even if it was not strictly necessary.

Tomalak
good answer!! thank you very much Tomalak!!
Can't answer them better myself. :)
Henry
Just want to add, the convention of calling it "init" may have come from CreateObject("java",...) uses .init() to call the java class's constructor.http://www.cfquickdocs.com/cf8/#CreateObject.java"Use the init method, with appropriate arguments, to call an instance of the class. For example:<cfset ret = myObj.init(arg1, arg2)>"
Henry
Per the note at the top of my answer, this is correct -- but there is some new exciting stuff coming out. Giving you a +1 in thanks for the content head-start. :)
Adam Tuttle
+5  A: 

Tomalak's answer is correct for ColdFusion 8; however things are changing a little bit with ColdFusion 9, due out later this year. (For the sake of completeness, I'll copy in Tomalak's answer and adjust as appropriate, so thanks for the head start. :))


Must I use the name init as the constructor name?

CF8: No, you can name the initialization function anything you like -- "init" is merely a convention. And it is not a true constructor, since it is not automatically called.

CF9: The answer is still "no," you don't have to. But it may behoove you to: This applies to creating objects within cfscript (or script-only components, which are also coming in CF9). The import and new keywords are being added, and you will be able to create an instance of an object like so:

import model.security.*;
userObj = new User();

This assumes that you have a User.cfc inside the model/security/ folder. In addition, CF9 will look for the existence of either a method named "init" or one with the same name as the object ("User", in this case), and implicitly call this as the constructor.

I'm not sure if passing in arguments to the new directive will forward them on to the constructor as you would see in Java/C. I assume it will, but I haven't seen any presentations or code examples that indicate one way or the other, yet; and haven't tested it myself because it just occurred to me while writing this. ;) (Another idea: I wonder if multiple constructors with different argument-sets will be supported. I doubt it, but it's a neat idea.)

I assume there is some implication that similar functionality might show up in CFML (the tag-based language, separate from cfscript), but nothing has been said about that at this point.

If I create an instance of the component without invoking the init method, what is returned?

The component instance is returned, as you'd expect it. The presence or absence of an init() function is completely irrelevant. There is no notion of static functions in ColdFusion components, you always get a fully constructed instance from GetObject("component", ...). (Not so for Java objects, which are constructed just before first use, if you forgot to/did not do it manually.)

Can I take the code in the init method out and delete the init method, then paste the code to the head of the component, is it have a same effect as the init constructor?

Yes, as long as the init() method did not take any parameters, there is no difference.

However, it is a convention to have a method called init() that returns the component instance ("this"). Even if it does nothing apart from "return this;". I'd stay consistent and add one to every component, even if it was not strictly necessary.

Adam Tuttle
+1 for the CF9 additions, very interesting. We're still on CF7 here, but if CF9 really comes out shortly (I have not tracked the release schedule) then I guess we skip CF8 altogether.
Tomalak
So far, all we've been told is that it will be released in 2009. It just went into public beta as of midnight + 1 second this morning (http://labs.adobe.com/technologies/coldfusion9/) along with ColdFusion Builder, the new official IDE (http://labs.adobe.com/technologies/coldfusionbuilder/).
Adam Tuttle
+1  A: 

Some 3rd party libraries/frameworks may expect init to be the constructor function (obviously, pre-CF9). For example, I believe ColdSpring looks for an method called initwhen attempting to pass in constructor arguments.

In response to Adam's answer, I'm pretty sure multiple constructors won't be part of CF9, since that would require method overloading, which I haven't heard anything about. The usual workaround is to take advantage of Duck typing and check arguments for type, and providing optional arguments (the ability to pass in named arguments makes this fairly easy to handle).

A link to Sean Corfield's presentation on Duck Typing: http://corfield.org/articles/ducktyping_cfunited.pdf

Ryan McIlmoyl
Yes, ColdSpring requires that the constructor be named Init. Duck typing isn't required to do various flavors of init -- just use optional parameters and pass them in by name (or with an argument collection). It was just a riff. :)
Adam Tuttle
Yeah, that's the approach I use as well. I only mentioned Duck typing for anyone with a Java mindset who wants to overload by type :)
Ryan McIlmoyl