Sam Farmer head shot

Sam Farmer

Growing up I never imagined I would play bass guitar for the Dave Matthews Band. And indeed it never happened.

But I have become a passionate and pretty good web developer.


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:

view plain print about
1component {
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:

view plain print about
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}

Now let's see what happens when we make an object and start interacting with it:

view plain print about
1<cfscript>
2//using the automatic constructor -- there is no init
3
//function in user
4
user = new user( firstname="Homer", lastname="Simpson" );
5
6//calling firstname will run getter function
7
//in CF9 this was user.getFirstname()
8
writeOutput( user.firstname );
9
10/*DISPLAYS:
11HOMER
12*/

13
14//calling name which directly references variables scope does
15
//not call getter function
16
writeOutput( user.name );
17
18/*DISPLAYS:
19Homer Simpson
20*/

21
22//now lets change the name and call it again:
23
user.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.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.9.7. Contact Blog Owner