It's not immediately clear how to differentiate between public and private properties in CFML CFCs (ColdFusion Components, which in turn are classes). Defining methods is more straightforward, in that they provide for public and private keywords to signal their scope. It would stand to reason that these scoping keywords might also be applicable for properties as well. After all, this is the approach taken with other languages such as Java and PHP.
Alas, it is not the case. And to compound matters, searching the Web doesn't really provide much clarification on the matter either. Answers range from, it's just not possible, to initializing variables in the constructor to make them act like private properties.
The examples provided here bear out that, CFML, at least running on Railo/Lucee, do in fact, support the scoping of properties. It works like this. Properties intitialized using the this.variableName form are public. Simply initializing a variable with no other qualifier makes it private.
Take the following component definitions to illustrate the case. They are followed by a CFML script that will instantiate the object and call a method that tries to reference a property, both using the public and private provisions.
The comments tell the story. When the comments talk about "the instance," they're referring to the instantiation of the PropertiesScoping object in the script example at the bottom.
Alas, it is not the case. And to compound matters, searching the Web doesn't really provide much clarification on the matter either. Answers range from, it's just not possible, to initializing variables in the constructor to make them act like private properties.
The examples provided here bear out that, CFML, at least running on Railo/Lucee, do in fact, support the scoping of properties. It works like this. Properties intitialized using the this.variableName form are public. Simply initializing a variable with no other qualifier makes it private.
Take the following component definitions to illustrate the case. They are followed by a CFML script that will instantiate the object and call a method that tries to reference a property, both using the public and private provisions.
The comments tell the story. When the comments talk about "the instance," they're referring to the instantiation of the PropertiesScoping object in the script example at the bottom.
/* =============================================================================
In this version, the *demonstration* property was set with private notation, so
property assignments from instances will have no affect on it.
============================================================================= */
component displayName="PropertiesScoping"
{
// Private scope
demonstration = "Initialized in class.";
// -^ This is referencing the private version.
public any function init()
{
// Assigned as private property.
demonstration = "Set from inside the init() method.";
// -----^ This is referencing the private version.
return;
}
public string function accessDemonstrationProperty()
{
return demonstration;
// ------------^ This is referencing the private version.
}
}
/* =============================================================================
In this version, the *demonstration* property was set as public. This way,
instances of the class will be able to write to the property.
Note that the two references prior to the accessDemonstrationProperty()
definition were overwritten by the assignment in the instance.
============================================================================= */
component displayName="PropertiesScoping"
{
// Public scope
this.demonstration = "Initialized in class.";
// -^ This is referencing the public version.
public any function init()
{
// Assigned as public property.
this.demonstration = "Set from inside the init() method.";
// -----^ This is referencing the public version.
return;
}
public string function accessDemonstrationProperty()
{
return this.demonstration;
// ------------^ This is referencing the public version.
}
}
/* =============================================================================
This time, the *demonstration* property will be initialized as public but will
be referenced in the accessDemonstrationProperty() as a private property.
The private property access has failed with the error message:
variable [DEMONSTRATION] doesn't exist
============================================================================= */
component displayName="PropertiesScoping"
{
// Public scope
this.demonstration = "Initialized in class.";
// -^ This is referencing the public version.
public any function init()
{
// Assigned as public property.
this.demonstration = "Set from inside the init() method.";
// -----^ This is referencing the public version.
return;
}
public string function accessDemonstrationProperty()
{
return demonstration;
// ------------^ This is referencing the private version.
}
}
/* =============================================================================
This version is the opposite of the previous example. In this one, all
assignments to *demonstration* are using private references, except the call to
the accessDemonstrationProperty() method. This is accessing the public property
as set in the instance, so it returns /that/ string.
============================================================================= */
component displayName="PropertiesScoping"
{
// Private scope
demonstration = "Initialized in class.";
// -^ This is referencing the private version.
public any function init()
{
// Assigned as private property.
demonstration = "Set from inside the init() method.";
// -----^ This is referencing the private version.
return;
}
public string function accessDemonstrationProperty()
{
return this.demonstration;
// ------------^ This is referencing the public version.
}
}
This is the script that can be used to demonstrate the classes above. Use them as separate classes to see how each of them behaves according to their descriptions.
<cfscript>
Example = new PropertiesScoping();
Example.demonstration = "Set from instance.";
dump(Example.accessDemonstrationProperty());
</cfscript>
Comments
Post a Comment