Jonathon,

PHP guy eh? In CF there's no way to have a function in a function. Same with
Java. Generally speaking, make another object. Or there's always passing
functions as arguments. See, what it sounds like is you're coming at the
problem backwards, however, that's not necessarily a bad thing. How about
some functional programming?

function add(n1,n2) {return n1+n2;}
function subtract(n1,n2) {return n1-n2;}

function doSomethingWith2Numbers(n1,n2, thingToDo) { return
thingToDo(n1,n2);}

writeOutput( doSomethingWith2Numbers( 5, 5, add) );
writeOutput( doSomethingWith2Numbers( 5, 5, subtract) );

(even runs on 6.1)

Now, like I said, this is backwards from most CF, but totally allowable by
the language.

As for including a UDF cfm file in your cfc - this is a great idea, but I am
interested in why you're going out on such a ledge, even if it's just
because you've done enough PHP to warp your brain ;)

-- 
nathan strutz
http://www.dopefly.com/


On 10/22/07, Jonathon Stierman <[EMAIL PROTECTED]> wrote:
>
> That is the crucial key I needed! Much easier than what I had tried to do
> with the meta data too -- bonus!
>
> >like a cfinclude file (even with a dynamic file name) from inside the CFC
>
> Is this to get around the inability to nest <cffunction> tags in CFC's?
> That's definitely a hurdle... any speculation as to why CF doesn't offer
> us
> a Function object we could create and modify, without having to use
> CF-tags
> or cfscript?  Or does it?  I'd prefer to create a blank function object,
> then fill it up with the details... Hmm.  Just how would I set the
> functionality of the function without a cffunction tag.  I'd almost
> need...
> another function :P  Well, alright, that's a little trickier than I had
> first thought.
>
> So it seems accepted practice to include a .cfm file from your component
> to
> get around that.  Anyone care to play devils advocate against that
> practice?
> If I'm dynamically writing the .cfm file content (which I will be doing),
> I
> then just include that .cfm file within my function and reference my
> function (stored as a variable) from there.  Interesting...
>
> Am I still on track, or wildly flailing in the wind? :)
>
> Jonathon
>
> -----Original Message-----
> From: Nathan Strutz [mailto:[EMAIL PROTECTED]
> Sent: Monday, October 22, 2007 1:16 PM
> To: CF-Talk
> Subject: Re: programmatically add functions to a CFC
>
> Jonathon,
>
> You're on the right track, but it's much easier than that. Here's some
> play
> code I just whipped up:
>
> <cfscript>
>   function getVariablesExternal() {return variables;}
>   function methodToInject() {return "This method was Injected";}
>   myCFC = createObject("component","test").init(); // get component
>   myCFC.getVariables = getVariablesExternal; // inject variable scope
> getter
> into "this" scope
>   myCFC.getVariables().injectedMethod = methodToInject; // inject a method
> into CFC "variables" scope
>   myCFC.getVariables().this.injectedMethod = methodToInject; // also into
> public "this" scope
>   writeOutput( myCFC.getVariables().privateMethod() ); // call private
> method from test.cfc
>   writeOutput( myCFC.injectedMethod() ); // call injected method
> </cfscript>
>
> It's really amazing the things you can do with this stuff :D
>
> PS here's my test.cfc:
>
> <cfcomponent>
>
>     <cffunction name="init" access="public">
>         <cfreturn this>
>     </cffunction>
>
>     <cffunction name="privateMethod" access="private">
>         <cfreturn "This is a Private Method!">
>     </cffunction>
>
>     <cffunction name="publicMethod" access="public">
>         <cfreturn "This is a Public Method!">
>     </cffunction>
>
> </cfcomponent>
>
>
> This is actually the concept of mix-ins, there has been some more formal
> code written about it, but you can do it like this, like a cfinclude file
> (even with a dynamic file name) from inside the CFC, you can get a struct
> of
> functions and loop over them, calling each or injecting them elsewhere,
> you
> can have a mixin() method that takes a function as an argument, and so on
> and so on. Really, lots of fun things here!
>
> --
> nathan strutz
> http://www.dopefly.com/
>
>
> On 10/22/07, Jonathon Stierman <[EMAIL PROTECTED]> wrote:
> >
> > Is it possible to programmatically add/update functions to an
> instantiated
> > CFC?
> >
> > I've made one attempt to add one by calling the getMetaData function,
> but
> > nothing I added to the meta data seemed to update the object's method
> > list.
> >
> > Is there another way to go about doing this, or am I on the right track?
> >
> > Jonathon
> >
> >
> >
> >
>
>
>
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Create robust enterprise, web RIAs.
Upgrade to ColdFusion 8 and integrate with Adobe Flex
http://www.adobe.com/products/coldfusion/flex2/?sdid=RVJP

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:291791
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to