What is the equivalent of static methods in ColdFusion?

后端 未结 2 1802
有刺的猬
有刺的猬 2021-02-19 23:37

In C#, I created static methods to help me perform simple operations. For example:

public static class StringHelper
{
    public static string Reverse(string inp         


        
2条回答
  •  轮回少年
    2021-02-20 00:15

    One way to create statics in ColdFuison is to put the function or variable in the metadata of the object. Its not perfect but like a static you don't have to create an instance of the object to call them and they'll last until the server is restarted so they are quite fast after the first call.

    Here's a quick snippet:

    component name="Employee"
    {
     public Employee function Init(){
      var metadata = getComponentMetaData("Employee"); 
    
      if(!structKeyExists(metadata,"myStaticVar")){
    
       lock name="metadata.myStaticVar" timeout="10"{
        metadata.myStaticVar = "Hello Static Variable."; 
       }
      }
    
      return this;
     }
    }
    

    More detail here: http://blog.bittersweetryan.com/2011/02/using-metadata-to-add-static-variables.html.

提交回复
热议问题