Ask a Question related to Coldfusion Component Development, Design and Development.
-
Mantalore #1
getters and setters
I am new to CFCs and I think I am missing the point of getters and setters A
lot of the text says that when writing a cfc we should write getter and setter
methods but it seems like a lot of extra work and I would like to know what the
benefits are. Why is it not a good idea to set the variables publicly and
access the cfc properties directly? I produced the following cfc:
<cfcomponent hint='I am the son' extends='Dad'> <cfset this.firstName='Rob'>
<cffunction name='getfirstname' returntype='string'> <cfreturn
this.firstName> </cffunction> </cfcomponent> Then I produced the following
test page: <cfobject component='Son' name='sonObj'> <cfoutput>
#sonObj.firstname#<br> #sonObj.getfirstname()#<br> <cfset
sonObj.firstname='Adam'> #sonObj.firstname#<br> #sonObj.getfirstname()#<br>
</cfoutput> Which produced the following output: Rob Rob Adam Adam I
realise that by scoping the firstname with 'this' that I am making it a public
variable which can be accesssed outside the object and that the benifit of
encapsulation is not gained but my question is why is this not done in the
examples of the web? By scoping the property in the 'variables' scope and
writing getters & setters we may get encapsulation but the ability of being
able to change properties remains via the getters & setters. Is it because
setters allow for validation that setting the value publicly does not? But if
so why write getters, they dont require validation. If variables are set
publicly are they open problems that I am not aware of?If so could some one
show me an example. Are there any good articles on the subject?
Mantalore Guest
-
When to use getters/setters with CFC's
I'm a bit confused as to when it is good practice to use getter/setter methods in CFC's and when to use the THIS scope to make the properties... -
[PHP-DEV] static property setters
Andi, Zeev, Mind if I add #define ZEND_STATIC_PUBLIC_PROPERTY(class_ptr, name, value) \ { \ char *_name = (name); -
maybenull #2
Re: getters and setters
two reasons stand out to me on why to use getters/setters in coldfusion
1) validation/behavior (you already mentioned this of course) - its covered
well at [url]http://naeblis.cx/rtomayko/2005/01/20/getters-setters-fuxors[/url] where
python, which handles this irksum pattern well, is compared to java.
2) tool/utility support - many tools automagically call getters and setters
for you, machii event-bean comes to mind; as for the coding of the getters and
setters, most good ide's, such as eclipse and intellij idea, will generate them
for each variable in your component. no coldfusion ide has reached this point
to my knowledge, but i do have an issue entered for this in cfeclipse.
maybenull Guest



Reply With Quote

