Skip to content

Latest commit

 

History

History
63 lines (47 loc) · 1.54 KB

implicit_property_functions.md

File metadata and controls

63 lines (47 loc) · 1.54 KB

Implicit Accessor Methods

Implicit Accessor Methods in Lucee allows treating getters and setter methods on a component as if they were public variables in the this scope. This enables the developer to gracefully wrap public variables in a function to validate them. To enable this feature, it requires an override in the Application.cfc

{% gist id="roryl/c8c33b03651b5c4641a4a711799db735",file="Application.cfc" %}{% endgist %}

``` component { this.triggerDataMember=true; } ```

Consider the following component:

{% gist id="roryl/c8c33b03651b5c4641a4a711799db735",file="basicComponent.cfc" %}{% endgist %}

``` component {

public function setMyValue(string){ this.myValue = ucase(arguments.string); }

public function getMyValue(){ return this.myValue; }

}

</noscript>

Normally, one would call the setter & getter methods, `setMyValue()` and `getMyValue()`, like the following:

{% gist id="roryl/c8c33b03651b5c4641a4a711799db735",file="default.cfm" %}{% endgist %}

<noscript>
myObj = new basicComponent(); myObj.setMyValue("foo"); echo(myObj.getMyValue()); ```

However, it is possible to access the variables in the public this scope, and Lucee will look for any setters & getters and call them instead, if they exist:

{% gist id="roryl/c8c33b03651b5c4641a4a711799db735",file="implicit.cfm" %}{% endgist %}

The above would output FOO

``` myObj = new basicComponent(); myObj.MyValue = "foo"; echo(myObj.MyValue); ```