什么是ColdFusion的静态方法相同呢? [英] What is the equivalent of static methods in ColdFusion?

查看:127
本文介绍了什么是ColdFusion的静态方法相同呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我创建了静态方法来帮助我进行简单的操作。例如:

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

public static class StringHelper
{
    public static string Reverse(string input)
    {
        // reverse string
        return reversedInput;
    }
}



然后在控制器中,我会简单地称它为使用:

Then in a controller, I would call it by simply using:

StringHelper.Reverse(input);

现在我使用的ColdFusion使用模型胶水,和我想要做同样的事情。不过,好像没有在ColdFusion的静态方法的概念。如果我创建一个这样的CFC:

Now I'm using ColdFusion with Model Glue, and I'd like to do the same thing. However, it seems like there's no concept of static methods in ColdFusion. If I create a CFC like this:

component StringHelper
{
    public string function Reverse(string input)
    {
        // reverse string
        return reversedInput;
    }
}



我只能通过创建的实例调用此方法的StringHelper 在控制器中,像这样的:

Can I only call this method by creating an instance of StringHelper in the controller, like this:

component Controller
{
    public void function Reverse()
    {
        var input = event.getValue("input");
        var stringHelper = new StringHelper();
        var reversedString = stringHelper.Reverse(input);
        event.setValue("reversedstring", reversedString);
    }
}



或者是有一些地方,我可以把'静该框架将创建的幕后实例氯氟烃这样我就可以使用它,就好像它是静态的,有点像佣工怎么夹的作品?

Or is there some place where I can put 'static' CFCs that the framework will create an instance of behind the scenes so I can use it as if it was static, kind of like how the helpers folder works?

推荐答案

不,你是正确的,有没有在ColdFusion的静态方法的概念。我想大多数人会通过使用解决这一问题的应用范围是应用程序启动时创建一个单独的工具。因此,在您App.cfc在onApplication开始你可能有:

Nope, you are correct, there is no concept of static methods in ColdFusion. I think most would solve this problem through the use a singleton utilities in the application scope that are create when the application starts. So in your App.cfc in onApplication start you might have:

<cfset application.StringHelper = createObject("component", "path.to.StringHelper") />



然后:

Then when you needed to call it from anywhere you would use:

<cfset reversedString = application.StringHelper.reverse(string) />



是的,这不是静态方法一样干净。也许有一天,我们能有像他们。但现在我认为这是接近你会得到。

Yeah, it's not as clean as static methods. Maybe someday we could have something like them. But right now I think this is as close as you will get.

这篇关于什么是ColdFusion的静态方法相同呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆