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

Skype Now Supports Emergency Calls In the US

With the latest round of updates to Skype , it was noticed today that the instant messaging and VoIP phone service now supports emergency calling in the United States. The release notes for the most recent update to the Android version of Skype indicated that the service now allows calls to be made to US emergency calling systems via 911.  Telephony services like Skype, Google Voice , and Vonage typically don't offer this emergency calling ability without some sort of intermediate step like deferring to the phone's default dialer, as is the case with Voice, or setting up a physical address location ahead of time which is how Vonage handles it. The indication is that Skype will handle the 911 calls natively, but when verifying with an account that does not have a Skype phone number assigned to it, it was found that the instructions in the FAQ explaining how to turn on emergency calling are not accurate. Emergency calling support in Skype for the United States Interestingly, th...

PHP built-in webserver and IPv6 addresses

Though it's difficult to confirm around the Web, it appears as though the IP address scheme the built-in PHP server uses depends on the host name that's provided.  If a fully-qualified domain name (FQDN) is used, such as server.domain.tld, then the built-in server will be listening for IPv6 based requests.  If the server is started with an IPv4 address identified as the host, then it will naturally listen for IPv4 based requests. The closest to anything official around the Web was a comment in the PHP docs that only seeded this conclusion, at best.  But here it is in any case. Built-in web server http://php.net/manual/en/features.commandline.webserver.php#120449 Ultimately, the following answer from Stack Overflow held the "universal" solution.  Thank you dew010. Running PHP 5.4 built-in web server outside localhost http://stackoverflow.com/a/8377378/2487485 Start your development server like: php -S 0.0.0.0:8989

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.