Ask a Question related to Coldfusion Component Development, Design and Development.

  1. #1

    Default 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 &amp; setters we may get encapsulation but the ability of being
    able to change properties remains via the getters &amp; 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

  2. Similar Questions and Discussions

    1. 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...
    2. [PHP-DEV] static property setters
      Andi, Zeev, Mind if I add #define ZEND_STATIC_PUBLIC_PROPERTY(class_ptr, name, value) \ { \ char *_name = (name);
  3. #2

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139