Just a note to those who attended my presentation - the code will be online Monday morning. I have net access, but won't be able to upload the zip till then.
For those who missed my presentation - I can now share some good news. The "page context" bug in CFCs is fixed and will be in the next update. Many other CFC bugs are fixed as well. There will also be support for super() (see my other blog entry), and you will be able to edit the core component.cfc that every CFC extends automatically. Yummy. I love CFCs and am happy to see them getting improved.
Archived Comments
Great presentation, Ray.
I had asked about the inclusion of a "proper" (i.e. ability to pass parameters) CFC constructor in the next version of ColdFusion, and while I understand the point you made about just using an init() method or the like, I still think the code would be a lot cleaner if we could do it as in Java.
im sure macromedia has its reasons for making cfc constructors the way they are. personally, i dont see a problem with using an init() method. i have found that using an init() method, specifically in being able to invoke that method more than once, to be a distinct advantage in many situations. i guess this could just be a matter of oppinion, though.
i have a question...
myobj = createobject(...).init(...);
does anyone suppose this syntax would be much different if we did have "proper" cfc constructors?
Well, I''d like syntax like so:
ob = createObject("component","name",struct)
Where struct would be a structure of args/values to pass into the constructor.
Is this so bad:
ob = createObject("component","name").init(struct)
after all, that''s what you need to do in Java for objects that have non-trivial constructors (i.e., that take arguments).
It''s not bad per se, but doesn''t this call end up using 2 objects? One object is created, that object is then used when init is called, and then init has to return a new instance of the cfc. Am I correct? If so, isn''t that a bit expensive? Yea, it''s not the end of the world, but still... :)
No, createObject() constructs the object and returns a reference to it. init() is called on that reference and returns "this" - which is a reference to the same object. That reference is bound to ob. It''s all references which are nice and lightweight.
Another useful technique is this inside mycfctype.cfc:
[cffunction name="newInstance" returntype="mycfctype"]
[cfreturn this]
[cffunction]
Then you can call it like this to create a new instance in environments where createObject() is disabled (shared hosts):
[cfinvoke component="mycfctype" method="newInstance" returnvariable="ob"]
That''s the equivalent of:
[cfset ob = createObject("component","mycfctype")]
Ah, for some reason I was thinking cfreturn this would return only the _data_, not the CFC itself. Sweet technique, Sean!