官术网_书友最值得收藏!

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
主站蜘蛛池模板: 株洲县| 娄烦县| 固始县| 新竹县| 宜兰县| 玉林市| 襄樊市| 自治县| 定远县| 塘沽区| 武功县| 沾益县| 九龙坡区| 邵东县| 邳州市| 玉屏| 射洪县| 宿州市| 通海县| 巩留县| 景德镇市| 清徐县| 万载县| 剑川县| 苏尼特右旗| 盐池县| 怀宁县| 宣威市| 凤山市| 神农架林区| 凌海市| 饶河县| 固始县| 鹰潭市| 肥西县| 汝城县| 东莞市| 平南县| 永安市| 宁化县| 英吉沙县|