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

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
主站蜘蛛池模板: 灯塔市| 新巴尔虎左旗| 湖北省| 积石山| 黄梅县| 鲁甸县| 米泉市| 林芝县| 河津市| 阳信县| 滦南县| 太康县| 肥乡县| 湘潭市| 定州市| 五莲县| 克拉玛依市| 平顺县| 湄潭县| 泽州县| 永嘉县| 曲松县| 卓资县| 库伦旗| 含山县| 广水市| 中牟县| 四平市| 舟曲县| 双流县| 夏邑县| 南陵县| 景谷| 苍溪县| 濉溪县| 丰顺县| 怀柔区| 湘乡市| 那坡县| 远安县| 乐业县|