Import and new operations using cfimport

CFScript supports import and new operations. "New" is now a keyword. However, it is not a reserved word, so you can use it as a variable name.

You can use the cfimport tag or import script operator to import a CFC. The import operation puts the contents of the specified component in the current name space, and caches the resolved component path in memory. The import action is effective on the current page only. If you import CFCs in Application.cfm, the CFC is not imported on other pages of the application.

You refer to the imported component directly without using a dot-delimited pathname. Execution time for cached components is faster than with CFCs that you do not import.

Note: The cfobject and cfobject tags and the CreateObject function also cache the resolved component path. They do not, however, invoke an initializer function.

In script the import statement has the following syntax:

import "cfc_filepath"

Quotation marks are optional for most paths. Surround the path in quotation marks if any directory or the CFC name has a hyphen.

The cfimport tag now supports importing CFCs and takes a path attribute to specify the path to the CF file to import.

Use the import function or cfimport tag with a path attribute on top of the page only. Using them elsewhere has the same effect as putting them on top of the page. Therefore, standard coding practice places the import tags or operators at the top of the file. The cfimport tag can precede a cfcomponent tag. The import CFScript statement must follow the component statement.

The ColdFusion Administrator Sever Settings > Caching page now has a Component Cache option, and a Clear Component Cache button. To prevent ColdFusion from caching resolved component paths, clear the Component Cache option. Click the Clear Component Cache button to remove any resolved component paths from the cache.

Note: In all cases, ColdFusion automatically imports the com.adobe.coldfusion.* name space for CFCs. You do not have to import this path explicitly.

The new operator creates an instance of a CFC. It is equivalent to the cfobject tags and CreateObject function. You can use new as a CFScript operator, or in assignment statements outside a CFScript block, such as in a cfset tag. ColdFusion does not have a corresponding cfnew tag.

The new operation has the following syntax:

cfObject=new cfcPath(constructorParam1,...)

or

cfObject=new cfcPath(arg1=constructorParam1Value,...)

If the folder name or CFC name has hyphen, use the following syntax:

cfObject=new "cfc-path"(constructorParam1,...)

If you use the import operator to import the directory that contains the CFC, the cfcPath value is the CFC filename. The constructor parameters can be positional or in name="value" format. When you use the new operator, ColdFusion does the following:

  1. Looks for an initmethod constructor method in the CFC. If found, ColdFusion instantiates the component and runs initmethod.

  2. If it does not find an initmethod constructor method, it looks for an init constructor method. If found, ColdFusion instantiates the component and runs initmethod.

  3. If neither method exists, the new operation instantiates the component but does not call a constructor.

Note: Only the new operator automatically invokes the initmethod or init function. The new operator returns the value returned by init or initmethod and if the return is void it returns the instance of the CFC. The cfobject tags and the CreateObject function do not invoke the function and you must explicitly call any custom initialization code.