intro to cfc’s - pete freitaginterfaces • new in cf8 • defines a set of functions that must...
TRANSCRIPT
![Page 1: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/1.jpg)
Intro to CFC’sPresented by Pete Freitag.
Foundeo Inc.
1
![Page 2: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/2.jpg)
What’s a CFC?
• ColdFusion Component
• Similar to a Class in OOP
• A Logical Grouping of data and functions (or methods)
2
![Page 3: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/3.jpg)
OOP, OMG!inheritance constructors polymorphism
interfaces overriding beans dao factories
gateways design patterns oop getterssetters super this class object orm
singleton abstraction couplingvoid arguments encapsulation
What’s all that fancy talk?3
![Page 4: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/4.jpg)
Words I may interchange
• Component = Object
• Function = Method
4
![Page 5: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/5.jpg)
Why use CFC’s
• Maintainability
• Reusability
• Quality
• Readability
• Separation of Responsibility
5
![Page 6: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/6.jpg)
Functions
• We need to know how to write a function before we can write a CFC.
• We must use the CFFUNCTION tag for CFCs.
6
![Page 7: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/7.jpg)
<CFFUNCTION>Common Tag Attributes
Attribute DescriptionName The name of the function.
ReturnType The data type (eg “string”) of the data that the function returns. Or void if it doesn’t return anything.
Output Yes/No this function outputs stuff to browser.
Hint Documentation for the function.
AccessOnly Applies to CFC’s. What code can call this function? Public, Private, Package, Remote
Roles For integration with CFLOGIN. Who can call this function?
7
![Page 8: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/8.jpg)
A Simple Function
<cffunction name="todayIs" returntype="string" output="false"> <cfreturn DateFormat(Now(), "dddd mmmm d, yyyy")></cffunction>
Data is returned from the function with the <cfreturn> tag.
8
![Page 9: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/9.jpg)
Passing Data into Functions
• The data that is passed into a function is called an argument
• We define arguments with the... you guessed it: <cfargument> tag.
9
![Page 10: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/10.jpg)
<CFARGUMENT>Common Tag Attributes
Attribute DescriptionName The name of the argument.
Type The data type (eg “string”) of the argument.
Required Yes/No this argument must be passed in.
Default If the argument is not passed in, this is the default value.
Hint Documentation about the argument.
10
![Page 11: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/11.jpg)
Function with Arguments
<cffunction name="myDateFormat" returntype="string" output="false"> <cfargument name="date" type="date" default="#Now()#"> <cfreturn DateFormat(arguments.date, "dddd mmmm d, yyyy")> </cffunction>
Arguments values are accessed as: arguments.name
11
![Page 12: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/12.jpg)
Variables in Functions
• Variables that are only used inside the function are called “local” variables. They MUST be defined below your arguments using the VAR keyword.
• Otherwise the variable is accessible outside of the function, you could overwrite another variable.
• Code example.
12
![Page 13: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/13.jpg)
Creating a CFC
• At their most basic level, a CFC is just a bunch of functions inside a <cfcomponent> tag.
• When you store data in a CFC, then they become powerful.
• You can have many instances of a CFC each having different data.
13
![Page 14: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/14.jpg)
CFC Files
• CFC’s are saved using the file extension .cfc the name of the file is also the name of the CFC.
14
![Page 15: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/15.jpg)
City.cfc <cfcomponent hint="I represent a city" output="false"> <!--- data contained in the City Component ---> <cfset variables.cityName = "Syracuse"> <cffunction name="getCityName" returntype="string" output="false"> <cfreturn variables.cityName> </cffunction> <cffunction name="setCityName" returntype="void" output="false"> <cfargument name="cityName" type="string" required="true"> <cfset variables.cityName = arguments.cityName> </cffunction> </cfcomponent>
15
![Page 16: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/16.jpg)
Invoking a CFC
• Three options:
• CreateObject()
• CFOBJECT
• CFINVOKE
16
![Page 17: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/17.jpg)
Invoking a CFC
<!--- CreateObject ---><cfset cityObject = CreateObject("component", "City")><cfoutput>#cityObject.getCityName()#</cfoutput>
<!--- OR CFOBJECT ---><cfobject component="City" name="cityObject"><cfoutput>#cityObject.getCityName()#</cfoutput>
<!--- OR CFINVOKE ---><cfinvoke component="City" method="getCityName" returnvariable="name"><cfoutput>#name#</cfoutput>
17
![Page 18: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/18.jpg)
Packages
• The folder that the .cfc file is located in is called it’s “Package”.
• If you are in the same folder/package you can reference other CFC’s simply by their file name. Otherwise use package notation.
• “folderA.folderB.MyCFCName” would be used for /folderA/folderB/MyCFCName.cfc
18
![Page 19: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/19.jpg)
Packages
• You can place CFC’s in the custom tag folder to have them accessible to all your sites.
• You can also setup mappings in the ColdFusion administrator.
• “myMappingName.MyCFCName”
19
![Page 20: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/20.jpg)
Example 4
20
![Page 21: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/21.jpg)
Example 5• Inheritance
• Defines a relationship between two components. Allows you to “inherit” code from the parent.
• Must be an “IS A” relationship.
• Book “is a” Product
• City “is a” State (Not True)
• City “has a” State
21
![Page 22: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/22.jpg)
Using Inheritance
• Use extends attribute of cfcomponent to specify parent component name.
• Use super keyword to refer to parent component.
• eg: super.methodName() to call a method on the parent (typically when overriding).
22
![Page 23: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/23.jpg)
Interfaces
• New in CF8
• Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements the interface.
• Unlike inheritance, you can implement many interfaces (you can only extend one component).
• Great For Building Component API’s
23
![Page 24: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/24.jpg)
Definitions
• Let’s go back to the beginning and define some of those terms.
24
![Page 25: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/25.jpg)
CFC’s Vs Other OOP
• Constructors (announced for CF9)
• Destructors (not supported)
• Overloading (not supported)
• Multiple Inheritance (not supported, thankfully)
• Interfaces (now supported in CF8)
25
![Page 26: Intro to CFC’s - Pete FreitagInterfaces • New in CF8 • Defines a set of functions that must be defined. If the component defines all those methods it is said that it implements](https://reader033.vdocument.in/reader033/viewer/2022051906/5ff8fcb751671909fb6590db/html5/thumbnails/26.jpg)
Thanks!Any Questions??
blog: petefreitag.com
26