can a cfc recursively create an instance of itself...
# cfml-general
s
can a cfc recursively create an instance of itself?
b
Explain? You don't "call a CFC", you call a method.
And if you're asking if a method can call itself-- isn't that the very definition of recursion?
oh, wait you said "create", not call
Yeah, would work too
s
Note that it would only be "recursively" creating an instance of itself if the constructor method created an instance of itself -- which is unusual, probably not the scenario you're actually asking about anyway, and yes it would still work.
b
Mmm...recursion.
s
Follow up question how would you call yourself.
Copy code
new …
b
using the name of the CFC like always.
Copy code
new com.foo.Bar();
You don't create a new instance of a CFC in CFML from a reference to another instance. It's just by string name.
s
I didn’t know if the cfc was self aware
b
It isn't
I think you're overcomplicating this. You can create an instance of a CFC anywhere you like, even if that "anywhere" happens to be inside itself. Nothing in CFML prevents this.
The
new
is now "aware" of anything. It's just code.
Now, if you want to be dynamic, you can get the name of the cfc from its metadata.
Copy code
getMetadata(this).name
s
Just code (methods) and data (properties / variables / this). Objects -- instances of CFCs -- have no context really: methods are just functions that have access to a specific blob of data (under the hood, methods "in" an object are just regular functions that have an implicit/invisible first argument that the object itself (or at least its data) is passed. So
foo.bar(1, 2, 3)
is really
bar(foo, 1, 2, 3)
behind the scenes and the
foo.bar()
notification just provides a sort of scope for which
bar
function gets called.
s
I had no idea, That is interesting
last one…. maybe. can you call static methods without instantiating the cfc first, or using cfinvoke?
Copy code
mycfc.methodname('test')
i do see this
Copy code
MyFunkyCalculator::calculateValues( 1 );
does that work across engines?
a
Yes. By definition static methods belong to the class/cfc, not an object. That's what static means in this context.
👍 1
One can also call a static method on an object, it's still calling the class's method, not the object's.
j
I'm a little late to the party here. I can successfully call a static method from a class without instantiating an object as advertised. However, whenever I try to call a static method from an abstract class, CF blows up and states that it can't create the abstract component. Which is confusing to me since I'm calling a static function which by definition shouldn't need to create it. So calling static functions work until I define the class as abstract. What am I missing here?
Started a new thread over in cfml-general