To define an object in CFML, you need to create a file with the extension .cfc
, let's create the file Employee.cfc
and put the following code inside it:
<cfcomponent name="Employee"> ... </cfcomponent>
Now that we have created a basic Employee
component, we can call it from another CFML, .cfm
template in a number of ways, firstly with the <cfobject>
tag:
<cfobject component="Employee" name="myEmployee"> <cfdump eval=myEmployee>
Another way is to use the CreateObject()
function:
<cfset myEmployee = CreateObject("component", "Employee")> <cfdump eval=myEmployee>
You can also use the "new" keyword to create your object:
<cfset myEmployee = new Employee()> <cfdump eval=myEmployee>
All of the methods above will display the following output:
So far, we don't see much in our Employee object, so let's change Employee.cfc
to have a few properties:
<cfcomponent name="Employee"> <cfset this...