Skip to main content

Public versus private properties in CFML

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.

/* =============================================================================
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

Popular posts from this blog

Remove control of Chrome being managed by organization on personal devices

Chrome may indicate that it's being managed by a user's organization. This warning is provided by the Chrome Policies feature of the browser. To know if an instance of Chrome is managed by an organization, there will be an entry at the bottom of the browser’s hamburger menu (three dot menu), on the right side of the browser window that reads, "Managed by your organization." This is likely due to an entry in the Chrome Policies listing, which can be found by loading this page in the browser: chrome://policy The policies listed in this section are stored in the computer's file system in one of the following locations as JSON files. /usr/share/chromium/policies/managed /usr/share/chromium/policies/recommended Remove the offending JSON files, and click the Reload policies button. The Managed by your organization entry in the browser menu should be gone. A notice like this on instances of Chrome for work, school, library, or other devices that belong to an organizati...

Allow Windows authentication using SQL Server driver with DBeaver

DBeaver will allow Microsoft Windows single sign on access when connecting to Microsoft SQL Server using the SQL Server driver (rather than jTDS ).  From the driver properties settings, set the integratedSecurity flag to true . Open the Connection configuration panel and choose the Driver properties section. Set the integratedSecurity flag to true . A subtle, but important step is to not provide username and password credentials to the connection.

Connecting to SQL Server with jTDS using Windows Authentication

UPDATE:   At one point the tip in this article to set the USENTLV2 flag to true was accurate, but it appears it is no longer necessary, at least as far as DBeaver 's use of the driver is concerned.  If it solves an authentication problem, then great.  The expectation is that, an update in one of the packages that make this sort of connection possible has resolved the issue that at one time made this flag necessary. Connecting to Microsoft SQL Server using jTDS with a username and password that's managed by the database manager is pretty straightforward.  It may become a little more problematic however, when trying to use Windows authentication when connecting from a Windows machine. Do not supply a username and password. Set the USENTLMV2 property to true. NOTE:   Step 1 assumes a connection being made from a Windows machine.  If the connection is being made from something else, user credentials may actually be necessary. If the connect...