Depends on how you create your instances - new Foo(),
createObject(“componen”, "Foo).init(), <cfobject…> and <cfinvoke…> have
different behaviour. Which way do you use?
I would recommend new Foo(), because it best suits OO programming.
component{
variables.blah = "test";
function init(required numeric bar, array baz){
variables.bar = bar;
variables.bazlen = baz.len();
return THIS; //required in Lucee - while ACF returns this if
nothing else is returned
}
array function something(){}
}
The init method acts as constructor, everything outside of the functions
acts as *pseudo-constructor *(variables.blah=“test”).
new Foo( 1, [a,b,c] )
Creates the instance calls the pseudo-constructor and the *constructor *and
returns the instance.
createObject(“component”).init( 1, [a,b,c] );
Creates the instance calls the pseudo-constructor and the *constructor *and
returns the instance.
createObject(“component”);
Creates the instance calls the pseudo-constructor and returns the
instance.
Read the docs for cfobject
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f6e.html
and cfinvoke
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7e0a.html.
These tags are old, I wouldn’t use them anymore.
component{
array function init(required numeric bar, array baz){
return baz;
}
}
As the constructors in Lucee/ACF are no real constructors, but (almost)
normal methods, you can return what you want.
I think you shouldn’t use this - new Foo() should always return the
instance.
component{
*package* function init(required numeric bar, array baz){
return this;
}
}
new other.package.Foo()
ACF: Throws an exception with the message: Can not access a package method
from outside…
Lucee (Bug): Creates the instance, calls the pseudo-constructor and
returns the instance.
component extends=“Bar” {
}
The Bar.init() method is inherited and called as described above.
If you implement the init-Method in Foo you have to call super.init()
manually.
In my opinion, the constructor behaviour is pretty obscure in CF. The main
reason is that CF was not designed as an OO language. I hope we can fix
this in the future.Am Freitag, 27. März 2015 11:20:58 UTC+1 schrieb Siegfried Wagner:
Hi,
I looked through existing discussions if I could find something to clarify
my question about how the init method in components is called with Lucee
but couldn’t find much.
Btw. I think there are a lot of discussions here not focusing on specific
topics but containing a total mixture of everything that comes to mind
(especially in threads about the “future of Lucee” ;-))
My questions: how are init methods of components called with Lucee? With
creation of the object?
How do you pass a parameter or pass a return value that is not the object
(=this)?
I’m a little bit confused because during my research on the topic, I saw
so many different interpretations of how the init method is called and the
parameters are processed through different ACF versions and where it is
called implicitly on creation and where not.
Thanks a lot for clarifying!
Siegfried