4 Replies Latest reply on Aug 15, 2007 4:05 PM by Newsgroup_User

    learning CFCs

    TomD
      I am trying to learn CFCs I am working through tutorials in a book from 2002 called CodeRutters "Discovering CFCs -- ColdFusion MX Components" by Hal Helms and Benjamin Edwards put out by Techspedition and I am working a tutorial in a chapter about inheritance.

      The point is to pass arguments using two different classes: 5Employee and 5Person (I put the 5 in front to find the files easily in my directory) here is the code

      5Person.cfc:
      <cfcomponent>
      <cffunction name="new">
      <cfargument name="firstName" required="no" type="string" default="null">
      <cfargument name="lastName" required="no" type="string" default="null">
      <cfargument name="yearBorn" required="no" type="string" default="null">
      <cfargument name="subtype" required="no">

      <cfset this.firstName = arguments.firstName>
      <cfset this.lastName = arguments.lastName>
      <cfset this.yearBorn = arguments.yearBorn>

      <cfif IsDefined('arguments.subType')>
      <cfset child = arguments.subType>
      <cfset StructAppend(child,this,'yes')>
      <cfreturn child>
      </cfif>
      </cffunction>

      <cffunction name="getName">
      <cfreturn this.firstName + ' ' + this.lastName>
      </cffunction>

      <cffunction name="setName">
      <cfargument name="firstName" required="yes" type="string">
      <cfargument name="lastName" required="yes" type="string">

      <cfset this.firstName = arguments.firstName>
      <cfset this.lastName = agruments.lastName>
      </cffunction>
      </cfcomponent>

      5Employee.cfc
      <cfcomponent hint="I am an Employee" extends="com.techspedition.CFCs.5Person">
      <cffunction name="new">
      <cfargument name="firstName" type="string" required="no">
      <cfargument name="lastName" type="string" required="no">
      <cfargument name="gender" type="string" required="no">
      <cfargument name="dateOfBirth" type="string" required="no">
      <cfargument name="department" type="string" required="yes">
      <cfargument name="dateOfHire" type="string" required="yes">
      <cfargument name="subtype" required="no">

      <cfinvoke
      component="com.techspedition.CFCs.5Person"
      method="new"
      subtype="#this#"
      returnvariable="aPerson"
      firstName="#arguments.firstName#"
      lastName="#arguments.lastName#"
      gender="#arguments.gender#"
      dateOfBirth="#arguments.dateOfBirth#">

      <cfset this = aPerson>
      <cfset this.employeeID = CreateUUID()>
      <cfset this.dateOfHire = arguments.dateOfHire>
      <cfset this.department = arguments.department>

      <cfif IsDefined('arguments.subType')>
      <cfset child = arguments.subType>
      <cfset StructAppend(child,this,'yes')>
      <cfreturn child>
      </cfif>
      </cffunction>
      </cfcomponent>

      5testEmployee.cfm
      <cfset maxie = CreateObject('component', '5Employee')>
      <cfset maxie.new('Maxie', 'Mumm', 'Female', '5/31/1958', 'Accounting', '12/25/1999')>
      <cfdump var="#maxie#">

      Here is the output:
      DEPARTMENT Accounting
      YEARBORN null
      FIRSTNAME Maxie
      LASTNAME Mumm
      EMPLOYEEID 6A8A1E0E-C6C9-9A59-B1D1970F03202050
      DATEOFHIRE 12/25/1999

      gender and dateOfBirth disappear into oblivion. Those to variables are only defined in 5Employee.cfc and they are lost in the dump....well his output in the book shows that they make it along with everything else...well I had a hunch and I changed the code in 5Employee.cfc:

      ...
      <cfinvoke
      component="com.techspedition.CFCs.5Person"
      method="new"
      subtype="#this#"
      returnvariable="aPerson"
      firstName="#arguments.firstName#"
      lastName="#arguments.lastName#">

      <cfset this = aPerson>
      <cfset this.employeeID = CreateUUID()>
      <cfset this.gender = arguments.gender>
      <cfset this.dateOfBirth = arguments.dateOfBirth>
      <cfset this.dateOfHire = arguments.dateOfHire>
      <cfset this.department = arguments.department>
      ...
      this worked. OK WHY is my question, my way makes more sense because essentially we are invoking the 'super class' 5Person.cfc and those variables are not included in 5Person, they are in 5Employee...but in the book, the way he did it, he included gender and dateOfBirth in the invoke statement and it worked...why? Have CFCs changed since 2002, if so what would be a better book I could use?