所以,假设我有一个组件实例:
foo.cfc
(提示:此文本涉及 IT 技术)
<cfcomponent>
<cffunction name="locateMe">
<cfreturn "I don't know where I live!">
</cffunction>
</cfcomponent>
还有另一个组件,fooParent.cfc:
<cfcomponent>
<cfset this.foo = createObject("component", "foo")>
</cfcomponent>
假设我以几种不同的方式创建了“foo”的实例:
<cfset myStruct = {}>
<cfset myStruct.foo = createObject("component", "foo")>
<cfset myFoo = createObject("component", "foo")>
<cfset myFooParent = createObject("component", "fooParent")>
<cfoutput>
#myStruct.foo.locateMe()#<br>
#myFoo.locateMe()#<br>
#myFooParent.foo.locateMe()#<br>
</cfoutput>
正如预期的那样,这将输出:
I don't know where I live!
I don't know where I live!
I don't know where I live!
我想知道的是,在foo.cfc中可能做些什么来告诉我关于它被调用的上下文信息(无论何种信息!)。由于所有东西最终都存在于某种作用域中,而且所有作用域都是一种对象,我的意思是我真的很想知道如何在给定的实例化对象内部确定包含对象。最终,构建foo.cfc的一些方式可以使输出类似于我上面的示例片段:
I live within a "class coldfusion.runtime.Struct" instance!
I live within a "class coldfusion.runtime.VariableScope" instance!
I live within a "component cfjunk.fooParent" instance!
每个值可能是通过检查传递给getMetaData
实际包含对象引用的结果来确定的。
更新 如评论中Micah所建议的,我已经添加了“Java”标签,因为我怀疑他可能是正确的,解决方案可能在使用Java进行内省方面。
更新
不要把这看成一个纯学术讨论,让我解释一下为什么我需要这个。
我正在使用CFWheels ORM和包含来获取对我的数据的引用,如下所示:
var user = model("User").findOne(where="id=123", include="AuthSource", returnAs="object");
这将返回一个对象,我可以像这样引用它:
user.id // property of the "User" model
user.reset() // method on the "User" model
user.AuthSource.id // property of the "AuthSource" model
user.AuthSource.authenticate(password) // method on the "AuthSource" model
现在,在我的“AuthSource.authenticate”方法中,我想了解包含我的“User”对象。否则,我将不得不像这样调用函数:
user.AuthSource.authenticate(user, password) // note the redundancy in the use of "user"
我应该能够依赖于通过用户对象调用AuthSource模型上的方法并从该方法内部实际读取该对象的事实。