- ColdFusion 9 Developer Tutorial
- John Farrar
- 631字
- 2021-08-05 16:16:37
Using an object constructor
We will modify our code just a little bit to correct the missing variable using the constructor init()
method. Modify the createObject
line by adding .init()
; to the end as shown in the following highlighted line. You will find that you can tack on a method you want to call at the time of creating an object, all on the same line. This is a common method among developers. When you use a constructor, you should always return the this
variable to the caller. this
is the variable scope of an object that refers to itself and this
returns a reference to the object itself. That will correctly pass the object back when you place the constructor at the end of the creation of the object. If you look at the init
method in the CFC, you will find that we include it with the<cfreturn this>
.
<!--- Example: 2_2b.cfm --->
<!--- Processing --->
<cfscript>
objProduct = createObject("component","product_1").init();
You will also notice that we are pulling the value of the name attribute of our product object with the method called get_name().
When you use a method, the rounded parentheses are standard. If you were passing values into the method, they would go in the parentheses. Values passed into a method are called arguments. Now, by adding the highlighted code you will see that we get a blank page for results. It is good that we are not getting an error, but one more thing is missing for completing our first test of our object attribute name:
<cfscript>
objProduct = createObject("component","product_1").init();
objProduct.set_name(name="Egg Plant");
result = objProduct.get_name();
</cfscript>
Add the highlighted line and run the now-working page. You are welcome to change the name of the product if Egg Plant isn't to your taste!

Now we can add the other attributes and we will have completed our first CFC. We need to add getters and setters for the description
and price
attributes of object classes. One of the things that using the variables does is allow the values set inside our object to persist from one method call to the next. There are ways to have both temporary values and persistent values that stay for the lifetime of the object. We will talk about that later in the chapter. Now add the following highlighted lines to the .cfc
and .cfm
pages:
<cfcomponent>
…
<!--- get/set attribute:description ---> <cffunction name="get_description">name="get_description"> <cfreturn variables.attributes.description> </cffunction> <cffunction name="set_description"> <cfargument name="description"> <cfset variables.attributes.description = arguments.description> </cffunction> <!--- get/set attribute:price ---> <cffunction name="get_price"> <cfreturn variables.attributes.price> </cffunction> <cffunction name="set_price"> <cfargument name="price"> <cfset variables.attributes.price = arguments.price> </cffunction>
</cfcomponent>
We will make a small number of changes to the code. We will set the value of all the object attributes through the methods this time. We will also pull the variables back out with one change. Rather than setting variables, we will use the object to output the values out directly in the content section.
<!--- Example: 2_3.cfm ---> <!--- Processing ---> <cfscript> objProduct = createObject("component","product_1").init(); objProduct.set_name(name="Egg Plant");"); objProduct.set_description(description="A plant with egg like fruit."); objProduct.set_price(price="2.57"); </cfscript> <!--- Content ---> <cfoutput> Name: #objProduct.get_name()#<br /> Description: #objProduct.get_description()#<br /> Price: #objProduct.get_price()#<br /> </cfoutput>
Now if you look at the results below and compare them to the previous code example, you will start to see one of the benefits of developing code with objects. We are able to think about our logic as an entity with methods we can request and attributes we can examine.

This does not mean we won't do any work with a database. It does mean we can start to separate more of our processing from our presentation. In fact, we can use the same object for both processing and presentation. It will get even more powerful as we go on. Here is a list of concepts we have seen so far in our code examples:
- Objects can be created from class files called CFCs
- Objects typically have a constructor that is run when an object is created
- Methods can have arguments to pass data into the object
- When methods are called, they can return a value to the caller which can be assigned to another variable or it can be used for output
- 基于元胞自動機的城市路網交通流建模與仿真
- Excel 2013使用詳解(修訂版)
- Procreate繪畫創作從入門到精通
- 邊做邊學:Photoshop圖像制作案例教程(第2版·Photoshop 2020)
- Oracle Siebel CRM 8 User Management: LITE
- AI繪畫:Stable Diffusion從入門到精通
- Photoshop CC 服裝設計經典實例教程
- IT Inventory and Resource Management with OCS Inventory NG 1.02
- 中文版After Effects 2022基礎教程
- Adobe創意大學Photoshop產品專家認證標準教材(CS6修訂版)
- 新印象Premiere短視頻拍攝+剪輯+特效關鍵技術
- Autodesk Ecotect Analysis綠色建筑分析應用
- PPT設計與制作實戰教程
- 中文版AutoCAD 2022從入門到精通
- Magento 1.3 Theme Design