Static support for components
Static support for components
Lucee 5 supports static variables and functions inside components.
Example:
component {
// inside the static constructor you can define static variables, this code is executed when the "class" instance is loaded
static {
susi=1; // written to the static scope (defining the scope is not necessary)
static.sorglos=2; // again written to the static scope
}
<span class="nv">public</span> <span class="nv">static</span> <span class="nv">function</span> <span class="nf">testStatic</span><span class="p">()</span> <span class="p">{</span>
<span class="p">}</span>
<span class="nv">public</span> <span class="nv">function</span> <span class="nf">testInstance</span><span class="p">()</span> <span class="p">{</span>
<span class="nf">static.testStatic</span><span class="p">();</span> <span class="c">// calling a static function</span>
<span class="nv">return</span> <span class="nv">static.sorglos</span><span class="p">;</span> <span class="c">// returning data from the static scope</span>
<span class="p">}</span>
}
The "static" constructor static {...}
is executed once before the component is loaded for the first time, so every component of the same type shares the same static scope.
You can use static functions and data as follows.
component Test {
static {
staticValue=1;
}
public static function testStatic(){}
}
Test::testStatic();
x=Test:: staticValue;