Static Scope in Components

edit

Static Scope in Components

Static variables and functions belong to the component itself, not to individual instances. Use them for shared state (counters, caches, config) or utility functions that don't need instance data. Static scope is similar to Application scope in lifespan, but isolated per component — unlike Server scope which is shared across all applications.

Call static members with :: syntax: MyComponent::myStaticFunction().

Static Variables

Shared across all instances and persist for the application lifetime:

component {
	static {
		counter = 0;
	}
	public function init() {
		static.counter++;
		dump( "Instance ##" & static.counter );
	}
	public function getCount() {
		return static.counter;
	}
}

Every new Counter() increments the same counter.

Static Functions

Call directly on the component without creating an instance:

// Greeter.cfc
component {
	public static function hello( name ) {
		return "Hello, " & name & "!";
	}
}
// Usage - no instantiation needed
message = Greeter::hello( "World" );

Static vs Instance

component {
	public static function utility() {
		return "I don't need instance data";
	}
	public function process() {
		return "I can access this and variables scope";
	}
}

Static functions can also be called on instances (same result):

obj = new Example();
dump( obj.utility() );      // works
dump( Example::utility() ); // same thing

Factory Methods with new self()

new self() creates an instance of the component the function was called on, not of the component the function is written in. A factory method in a base component therefore returns the subcomponent when it is called on the subcomponent:

// Base.cfc
component {
	public static function create() {
		return new self();
	}
	public function init( a = 0 ) {
		variables.a = a;
		return this;
	}
}
// Child.cfc
component extends="Base" {
}
// Usage
base = Base::create();   // a Base
child = Child::create(); // a Child

Arguments are passed to the constructor, as with any other new: new self( a=1 ).

  • It works in static and in instance functions, instance functions use the component of the instance (child.someFunction() creates a Child).
  • self is only a keyword directly after new. self as a function name, variable or argument name works as before.
  • new self() outside of a component throws an exception.
  • The same abstract and constructor rules as for new apply, new self() on an abstract component throws.

Mocking Static Functions

You can override a static function on an instance for testing without affecting the component:

obj = new Example();
obj.utility = function() {
	return "Mocked!";
};
dump( Example::utility() ); // "I don't need instance data" (original)
dump( obj.utility() );      // "Mocked!" (instance override)

When to Use Static

  • Utility functions - helpers that don't need instance state
  • Shared counters/caches - track across all instances
  • Constants - values that don't change
  • Factory methods - alternative constructors

See also