Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

question about ColdFusion component constructor name

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.

like image 831
user133580 Avatar asked Jul 10 '09 07:07

user133580


1 Answers

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.

like image 71
Tomalak Avatar answered Oct 13 '22 19:10

Tomalak