- 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
- Premiere Pro 2022短視頻剪輯、調色與特效制作實戰(全彩微課版)
- Scratch 1.4: Beginner’s Guide
- BPEL Cookbook: Best Practices for SOA/based integration and composite applications development
- 計算機·手機生活應用
- NHibernate 2 Beginner's Guide
- 二維計算機繪圖教程:二維CAD工程師取證全程指導
- Android User Interface Development: Beginner's Guide
- After Effects中文版入門、精通與實戰
- 輕松玩轉3D One AI
- Maya 2020基礎教材
- CorelDRAW 2020中文版入門、精通與實戰
- Unity 2020游戲開發快速上手
- ASP.NET Core 3從入門到實戰
- AutoCAD 2020中文版入門、精通與實戰
- JBoss RichFaces 3.3