How to call Static Methods on components in parent directories

I have the following file structure:

/
Person.cfc

/Database
PersonUtility.cfc

/Security
validatePerson.cfc

The ‘Person.cfc’ calls a static method in ‘PersonUtility.cfc’ using the following:

value = Database.PersonUtility::createUser();

The ‘createUser()’ method needs to call a static method in component:

validatePerson

So, the ‘createUser’ method includes the following:

v1 = Security.validatePerson::foo();

The method invocation above fails because it cannot locate the ‘validatePerson’ component.

This fails because the ‘PersonUtility’ component is located in the ‘Database’ directory.

How can I modify the code to provide the absolute path to the ‘validatePerson’ component?

a few options for cross-directory static calls:

  1. Full path from webroot: staticQ.DirB.CompB::bar()
  2. Relative path via function: getComponentStaticScope( "../DirB/CompB" ).bar()

The :: syntax is compile-time and only accepts dot-notation component paths. For relative paths with ../, use getComponentStaticScope().

Out of interest, why are you using static?

If it’s just “performance”, i would try and avoid the path lookup overhead, but benchmark none the less, remember, warm up the code, do several runs, lots of rounds, let the jvm do it’s magic before accepting the results

1. new DirB.CompB().bar2()              22ms   (new instance each call)
2. DirB.CompB::bar()                    8ms    (static ::)
3. getComponentStaticScope().bar()      12ms   (static via function)
4. comp.bar2() (reused instance)        5ms    (cached instance)
5. ss.bar() (cached static scope)       4ms    (cached static)

So static :: is faster than creating new instances each call, but reusing an instance beats it.

The real win is caching - whether you cache the instance or the static scope, they’re both faster. The lookup is what’s expensive, not static vs instance.

https://luceeserver.atlassian.net/browse/LDEV-5940

1 Like

I am using static because it seems simpler to call a static method as compared to creating an object and then calling a method.

Based on your information, I may rethink my approach.

Thank you for the response.

1 Like

Here’s my really quick n dirty src code for the stats above (java 21, 7.0.2.25-SNAPSHOT)

staticQ.zip (1.9 KB)

after a few more runs on my 6 year old core i7 laptop, my new 9950x3d Desktop might need to uses nanos!

1. new DirB.CompB().bar2()              13ms   (new instance each call)
2. DirB.CompB::bar()                    10ms   (static ::)
3. getComponentStaticScope().bar()      8ms    (static via function)
4. comp.bar2() (reused instance)        3ms    (cached instance)
5. ss.bar() (cached static scope)       3ms    (cached static)

BTW you can use dot notation to call static functions on an instance

1 Like