i have some questions about constructors in ColdFusion :
init
as the constructor name?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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With