10 Little Ones: What are Implicit Accessors?
ColdFusion 10 added implicit accessors and in this post I'll cover how to turn them on and use them.
Turning on implicit accessors is achieved via a straightforward setting in application.cfc:
1component {
2
3this.name = "implicitFun";
4this.invokeImplicitAccessor = true;
5
6}
2
3this.name = "implicitFun";
4this.invokeImplicitAccessor = true;
5
6}
Once on this means we can now access a getter and setter function of an object/cfc using the same syntax as if it where a struct. Let's look at a simple component called user.cfc:
1component accessors="true" {
2
3property firstname;
4property lastname;
5
6function getFirstname() {
7 return UCase( variables.firstname );
8}
9
10function getName() {
11 return variables.firstname & " " & variables.lastname;
12}
13
14}
2
3property firstname;
4property lastname;
5
6function getFirstname() {
7 return UCase( variables.firstname );
8}
9
10function getName() {
11 return variables.firstname & " " & variables.lastname;
12}
13
14}
Now let's see what happens when we make an object and start interacting with it:
1<cfscript>
2//using the automatic constructor -- there is no init
3//function in user
4user = new user( firstname="Homer", lastname="Simpson" );
5
6//calling firstname will run getter function
7//in CF9 this was user.getFirstname()
8writeOutput( user.firstname );
9
10/*DISPLAYS:
11HOMER
12*/
13
14//calling name which directly references variables scope does
15//not call getter function
16writeOutput( user.name );
17
18/*DISPLAYS:
19Homer Simpson
20*/
21
22//now lets change the name and call it again:
23user.firstname = "Lisa";
24writeOutput( user.firstname );
25
26/*DISPLAYS:
27LISA
28*/
29
30
31</cfscript>
2//using the automatic constructor -- there is no init
3//function in user
4user = new user( firstname="Homer", lastname="Simpson" );
5
6//calling firstname will run getter function
7//in CF9 this was user.getFirstname()
8writeOutput( user.firstname );
9
10/*DISPLAYS:
11HOMER
12*/
13
14//calling name which directly references variables scope does
15//not call getter function
16writeOutput( user.name );
17
18/*DISPLAYS:
19Homer Simpson
20*/
21
22//now lets change the name and call it again:
23user.firstname = "Lisa";
24writeOutput( user.firstname );
25
26/*DISPLAYS:
27LISA
28*/
29
30
31</cfscript>
As you can see from the code above when you call the implicit accessor on an object it calls the getter or setter function but within a cfc calling the variables scope directly calls the variable and not the getter or setter function.

There are no comments for this entry.
Add Comment Subscribe to Comments