Ostfriese
Ostfriese

Reputation: 67

What's the best scope for variables of an ancestor component

I'm currently writing a component which should be used as an ancestor for other components and I'm not sure what's the best variable scope for variables which should be only available inside of my component and its inheritances.

Other programming languages like Pascal have a "protected" access rule that would do the job but I'm new to CF and don't know yet what's their pendant.

Upvotes: 2

Views: 164

Answers (1)

jan
jan

Reputation: 2958

ColdFusion doesn't have a protected scope. The variables scope is only accessible for the component itself and its extended components. The variables scope is your best option.

Example:

Component A:

component  output="false"
{
    variables.name = "John";
}

Component B (extends ComponentA):

component  extends="ComponentA" output="false"
{

    remote void function test(){
        writeDump(variables.name);
    }
}

new ComponentB().test(); will dump "John";

Accessing variables scope from outside the component throws an error:
writeDump(b.name); or writeDump(a.name); will throw an error (name is undefined)

Upvotes: 2

Related Questions