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

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

HP webOS interface kudos

UPDATED 06/07/2013 Pandora no-longer makes the webOS version of its player available. While quite obviously a piece rooted in opinion, a subtle nod is offered to Palm for the excellent interface work it did with webOS .  Since it’s initial release with Sprint in June 2009, Palm quickly slipped into relative oblivion.  With the recent purchase of the company by HP coupled with new product releases set for the summer, webOS may become relevant once again.  If so, then perhaps a whole new lot of users will come to appreciate the consideration that was taken with the webOS user interface (UI). An example is the popular webOS application for the even more popular Pandora Internet radio service.  And as it turns out, the company has a collection of screen captures for the various mobile platforms that have native applications for the service.  This happens to double as a quick way to compare the interface elements of other platforms by using the same applic...

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.