• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

learning CFCs

New Here ,
Aug 15, 2007 Aug 15, 2007

Copy link to clipboard

Copied

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?
TOPICS
Advanced techniques

Views

360

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 15, 2007 Aug 15, 2007

Copy link to clipboard

Copied

Yes, CFC's and the recommendations have changed since then. I don't know of a good CFC book but I would start by reviewing these articles paying special attention to:

- Usage of an init() function
- Usage of the "super" keyword for extended components
- Usage of "variables" scope for storing instance data
- Difference between the "this" scope and the "variables" scope
- Usage of keyword "VAR" for local function variables
- Usage of "returnType" attribute

http://www.oreillynet.com/pub/a/javascript/2003/09/24/coldfusion_tips.html
http://livedocs.adobe.com/wtg/public/coding_standards/contents.html
http://livedocs.adobe.com/coldfusion/7/htmldocs/00001022.htm

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 15, 2007 Aug 15, 2007

Copy link to clipboard

Copied

thank you for the articles, finding information on CFCs is daunting, there just isn't much out there like there is everything else

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 15, 2007 Aug 15, 2007

Copy link to clipboard

Copied

There was at least one book hastily released shortly before CFMX6.0 was
released that had a lot of bad advice in it, and I recall Hal Helms being
the writer. This could be it. Steer clear of it. Throw it out. It's not
Helms' fault: CFMX's first release was rubbish, and none of the CFC stuff
really worked properly until CFMX6.1.

--
Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 15, 2007 Aug 15, 2007

Copy link to clipboard

Copied

LATEST
> thank you for the articles, finding information on CFCs is daunting, there just isn't much out there like there is everything else

Livedocs covers pretty much all of what you'd need to know vis-a-vis CFCs
in particular. Some knowledge of OO concept would help (google, if you're
ont comfortable with this), but one must bear in mind that CF is not
completely OO: it simply implements some OO concepts. This is mostly a
good approach to OO, in my view: al lot of it is dogma for the sake of
dogma.

--
Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation