使用 cfinvoke 和 createObject 运行组件函数有什么区别?

发布于 2024-10-10 15:28:05 字数 136 浏览 8 评论 0原文

在我公司的代码中,我经常看到通过初始化该组件的对象并调用该对象的方法来使用组件文件。然而,在我看来,使用 cfinvoke 方法似乎更直接,特别是当仅使用组件文件中的一种方法时。这两种调用组件函数的方法有什么区别,各自的优缺点是什么?我什么时候应该使用哪个?

In my company's code, I've often seen component files used by initializing an object of that component and calling the methods off the object. However, it seems to me somewhat more straightforward to use the cfinvoke method, especially when only using one method from the component file. What are the differences between these 2 methods of calling a component function and what are the pros/cons of each? When should I use which?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

姜生凉生 2024-10-17 15:28:06

使用 createObject() 的另一个好处是您可以链接 init() 方法,例如

<cfset myObject = createObject("com.path.MyObject").init() />

如果您的 init() 返回 this 如果您不需要再次使用该对象,您可以进一步链接该方法:

<cfset functionResults = createObject("com.path.MyObject").init().myFunction() />

值得指出的是,在 CF 9 中您可以使用 new (咳咳) new创建对象的语法。例如,要创建与上面相同的对象并调用它的 init() 我可以这样写:

<cfset myObject = new com.path.MyObject() />

它很简洁,我喜欢执行此操作的选项。在我看来,CF 正在朝着正确的方向发展,具有这样的功能。

One other benefit of using createObject() is that you can chain the init() method, e.g.

<cfset myObject = createObject("com.path.MyObject").init() />

And if your init() returns this you can go further and chain the method if you don't need to use the object again:

<cfset functionResults = createObject("com.path.MyObject").init().myFunction() />

It's worth pointing out that in CF 9 you can use the new (ahem) new syntax to create objects. For example to create the same object as above and call it's init() I can write:

<cfset myObject = new com.path.MyObject() />

It's neat and I like the option to do this. CF is moving in the right direction in my opinion with features like this.

牵你手 2024-10-17 15:28:06

cfinvoke 只能在标签中使用。

createObject 可以在标签和标签中使用。 cfscript 并且在我看来往往更薄/更容易阅读。

直到最近,我还避免使用 cfinvoke,因为我发现它“笨重”,但它的优点是您可以动态循环 CFC 中的方法。在 createobject 中你不能。

例如,如果我有一个 CFC,它具有方法 - method1、method2、method3、method4。我可以像这样循环它们:-

<cfloop from="1" to="4" index="element">
   <cfif structKeyExists(this,'getMethod#element#')>
<cfinvoke component="#this#" method="getLine#local.element#" returnVariable="methodValue"></cfinvoke>
<cfset arrayAppend(myArray,methodValue) />
   </cfif>

--

另一件需要注意的事情是,某些共享主机会锁定 createobject。主要是因为它提供了对下面的 Java 的访问。

cfinvoke can only be used in tags.

createObject can be used in both tags & cfscript and tends to be a bit slimmer / easier to read IMO.

Until recently I avoided using cfinvoke because I found it "bulky" but a pro of it is you can dynamically loop over the methods within a CFC. In createobject you can't.

So if for example I've got a CFC which has the methods - method1, method2, method3, method4. I can loop over them like so:-

<cfloop from="1" to="4" index="element">
   <cfif structKeyExists(this,'getMethod#element#')>
<cfinvoke component="#this#" method="getLine#local.element#" returnVariable="methodValue"></cfinvoke>
<cfset arrayAppend(myArray,methodValue) />
   </cfif>

--

Another thing to note is that some sharing hosts lock down on createobject. Mainly because of the access it gives to the underlining Java.

行至春深 2024-10-17 15:28:06

您几乎已经自己回答了这个问题:从表面上看,人们可能会说,如果您只在页面上调用一个方法,那么在 CFINVOKE 中一举完成(它实例化 CFC 并调用一个命名方法)是有意义的。当然,如果您要在一页上调用 CFC 的多个方法,那么分离这些步骤是有意义的(使用 createobject 函数或 cfobject 标记实例化 CFC,然后调用该对象(指向 CFC 的指针)中找到的方法) ,这样您就不必多次支付实例化成本。

但请记住,如果经常调用该页面,则保存实例化 CFC 的结果也可能有意义,以便可以在对该页面的后续请求中重用它。您可以通过将其(cfobject/createobject 的结果)存储在共享范围中而不是本地变量中来实现这一点:无论是服务器、应用程序还是会话,基于“谁”将从这种重用中受益。当然,您有责任以编程方式处理/决定保存此“缓存”CFC 实例多长时间。

同样重要的是,当您以这种方式保存 CFC 实例时,您会更容易受到“var 范围错误”的影响,这基本上是您需要更加小心地对在 CFC 中创建的任何局部变量进行 VAR 操作。我不会尝试对此进行详细说明,而是会指出我在此基础上创建的元资源:

http://www.carehart.org/blog/client/index.cfm/2010/3/4/resources_on_the_var_scope_problem

希望有所帮助。

You've nearly answered it yourself: on the surface, one could say that if you will be calling only one method on a page, then doing in one fell swoop in CFINVOKE (which instantiates the CFC and calls the one named method) make sense. And certainly if you would call more than one method of the CFC on a page, then separating the steps makes sense (instantiate the CFC with the createobject function or cfobject tag, then invoke methods as found in that object, a pointer to the CFC), so that you don't pay that instantiation cost more than once.

But do keep in mind that if the page is called often, it may make sense also to save that result of instantiating the CFC, so that it can be reused on a subsequent request to the page. You would do that by storing it (the result of cfobject/createobject) not in a local variable but instead in a shared scope: whether server, application, or session, based on "who" would benefit from such reuse. Of course, it's then incumbent on you to programmatically handle/decide how long to save this "cached" CFC instance.

As important, when you save a CFC instance this way, you become more susceptible to the "var scope bug", which basically is that you need to be still more careful to VAR any local variables you create in the CFC. Rather than try to elaborate more on that, I'll point out a meta-resource I created on that:

http://www.carehart.org/blog/client/index.cfm/2010/3/4/resources_on_the_var_scope_problem

Hope that helps.

小清晰的声音 2024-10-17 15:28:06

然后,我将直接向您指出 Google:

http://www .google.com/search?q=cfinvoke+vs+createobject

有一些细微的差别(IE: 能够处理动态方法名称),但本质上它只是沸腾取决于个人喜好。好吧,事实上您不能通过 使用

Rather then rehash this discussion I'll just point you towards Google:

http://www.google.com/search?q=cfinvoke+vs+createobject

There are some subtle differences (IE: <cfinvoke> is capable of handling dynamic method names) but essentially it just boils down to personal preference. Well, that and the fact that you can't use <cfinvoke>via <cfscript>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文