Coldfusion的CFC没有constructor (查找过资料,没有找到。)。如果想在使用一个属性变量的之前把它初始化, 对coldfusion来说好像有些困难。今天我就碰到这样一个问题。
原来的做法是把一个固定的值赋给属性变量。如下面的例子:
<!--- abc.cfc --->
<cfcomponent>
<cfset this.abc = "1,2,3">
</cfcomponent>
<!--- test.cfm --->
<cfobject name="obj" component="abc">
<cfset a = obj.abc>
<cfoutput>#obj.abc#</cfoutput>
这时,显示出来的就是固定的赋值的内容。
但是公司要求这个变量是动态的,就是说,abc的值要动态产生。
如何还是以<cfset a = obj.abc>的形式调用呢?
解决方案如下:
<!--- abc.cfc --->
<cfcomponent>
<cfset this.abc = getabc()>
<cffunction name="getabc">
... 这里可以是数据处理过程 ....
<cfreturn "1,2,3">
</cffunction>
</cfcomponent>
这样,处理过程可以和原来一样了。当然,还可以加上一些代码,那么abc的值就只赋值一次,就可以全程调用。
<!--- test.cfm --->
<cfobject name="obj" component="abc">
<cfset a = obj.abc>
<cfoutput>#obj.abc#</cfoutput>