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...