Recently I talked about conforming to using init() methods in my components. I had planned to use createObject().init() to make the call in one nice quick line.

There's just one problem, createObject() won't work for me in my preferred site structuring.

I like to house all my application-level and session-level variables in separate include files in an includes/ directory beneath the web root. In Application.cfc, I call to those files in onApplicationStart() and onSessionStart(). It's a nice way for me to compartmentalize code and keep Application.cfc from getting too messy. I've done this for years now in dozens of sites.

I keep all my components in a components directory on the same level as the includes directory. I initialize the components in the application scope because they change so little (only when a code release is done) that it makes sense to keep them there to limit overhead by only creating the object one time.

Until recently I would use CFOBJECT to create the component variable. I would call it like this:

view plain print about
1<cfobject name="application.mySite.cfc.myComponent" component="components.myComponent" />

Never a problem. I try to change it to:

view plain print about
1<cfset application.mySite.cfc.myComponent = createObject("component","components.myComponent").init() />

and it blows up with the error "Could not find the ColdFusion component or interface components.myComponent".

For an hour I could not figure out why until digging enough showed me one huge difference between createObject() and CFOBJECT. While CFOBJECT is smart enough to know that the dot notation of the directory structure begins at the site root, createObject() only looks from the location of the template the call resides in. In this case it's the includes directory.

For kicks I added the createObject() call directly into the onApplicationStart() function in Application.cfc. It worked fine, confirming my lesson of the night that it has no capability to traverse up a directory structure, only down.

So it looks like I'll be sticking with CFOBJECT after all, at least until Adobe decides to give createObject() a little more flexibility. What stinks is that I'll have to make separate calls to the init() functions to get them run. Kinda defeats the purpose of them a little bit I think.