Ummm, i wonder if Phillip is talking about inheritance in an OO sense or
if he means "I'd like the variables that are available in component A,
which calls component B, to be available in component B." If component A
calls a method in component B, then it stands to reason that component B
might be composed within A.
Something you can do that can come in handy sometimes when using
composition is to pass a reference to the parent component (the
"container" when using composition) into the child component, the one
that's inside the parent.
Here's a "quick" example to explain what i mean:
<cfcomponent displayname="A" hint="I'm the containing object">
<cffunction name="init">
<cfset setGreeting("Hey there!") />
<!--- compose a child component inside a parent component
and pass a reference to the parent into the child using
"this" --->
<cfset variables.B = createObject('component','B').init(this) />
<cfreturn this />
</cffunction>
<cffunction name="getGreeting">
<cfreturn variables.Greeting />
</cffunction>
<cffunction name="setGreeting">
<cfargument name="greeting" />
<cfset variables.greeting = arguments.greeting />
</cffunction>
</cfcomponent>
<cfcomponent displayname="B" hint="I'm inside of A, but strangely
enough, a reference to A is inside of me! Whoa, that's cool!">
<cffunction name="init">
<cfargument name="A" />
<cfset var parentGreeting = arguments.A.getGreeting() />
<!--- the parentGreeting is now available here inside the child
--->
<cfreturn this />
</cffunction>
</cfcomponent>
Carrying the example a little further, if you wanted, you could
manipulate the parent greeting from within the child object, B.
Let's modify the init() method of B to try that out ...
.....
<cfset var parentGreeting = arguments.A.getGreeting() />
<!---
the parentGreeting is now available here inside the child,
but perhaps we should be a little more formal,
we've only just met.
--->
<cfset = arguments.A.setGreeting("Good day, sir.") />
.....
Now if we called a.getGreeting(), we'd get the more formal version.
Maybe we should retain the original greeting in the child, adding
setOriginalGreeting() and getOriginalGreeting() methods to B.
Let's modify the init() method of B to take care of that.
.....
<cfset var parentGreeting = arguments.A.getGreeting() />
<!--- the parentGreeting is now available here inside the child
perhaps we should be a little more formal, we've only just met
--->
<cfset = arguments.A.setGreeting("Good day, sir.") />
<cfset setOriginalGreeting(parentGreeting) />
.....
And then if we wanted, we could access the original greeting from A,
because we have a reference to B in the variables scope of A. Let's
modify A a bit ...
<cfcomponent displayname="A" hint="I'm the containing object">
<cffunction name="init">
<cfset setGreeting("Hey there!") />
<!--- compose a child component inside a parent component
and pass a reference to the parent into the child using
"this" --->
<cfset variables.B = createObject('component','B').init(this) />
<cfreturn this />
</cffunction>
<cffunction name="getGreeting">
<cfreturn variables.Greeting />
</cffunction>
<cffunction name="setGreeting">
<cfargument name="greeting" />
<cfset variables.greeting = arguments.greeting />
</cffunction>
<cffunction name="getOriginalGreetingFromB">
<cfreturn variables.B.getOriginalGreeting() />
</cffunction>
</cfcomponent>
Calling a.getOriginalGreetingFromB() would return "Hey there!"
So there you have it. The Tao of This.
:) Nando
Phillip Senn wrote:
Q: But what about when I want a component to inherit the variables that are
in a calling component?
-----Original Message-----
From: Phillip Senn [mailto:[EMAIL PROTECTED]
Sent: Friday, March 10, 2006 9:19 AM
To: '[email protected]'
Subject: RE: [CFCDev] cfc scope issues
I think I understand your question, because I had the same kind of question
before. In essence, it's this:
Q: "I know all the variables are supposed to be passed into a cfc as
parameters, but aren't there any exceptions?"
A: No, there are no exceptions.
Q: Why?
A: Because you would be assuming that your cfc is being used by only your
application. If you require all the variables to be passed as parameters,
you could use your cfc in any application.
Q: What about Application scoped variables.
A: Pass them as parameters.
Q: What about constants?
A: Pass them as parameters.
Q: What about...
A: Shhhh!
Q: But...
A: Shhhh!
Q: I...
A: Shhhh!
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to
[email protected] with the words 'unsubscribe cfcdev' as the subject of the
email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting
(www.cfxhosting.com).
An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to
[email protected] with the words 'unsubscribe cfcdev' as the subject of the
email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting
(www.cfxhosting.com).
An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]