Quick Question I hope

1 view
Skip to first unread message

DreamKnight

unread,
Nov 25, 2009, 3:30:59 AM11/25/09
to CFCDev
When I instantiate an object through the variables scope either in the
psuedo or init constructor, it can't find any methods when called
through that variable in any methods (gives as cferror). What I'm
trying to do is be able to use the object methods within any of the
current objects in the cfc. I'm sure there's other better ways to do
this and if you got a suggestion on how, that'll be great.

Thanks!

Looks something like this:
<cfcomponent>
<cfscript>
variables.getclass = createobject("component", "com.path.class").init
();
</cfscript>

<cffunction name="init" ...>
<cfreturn this />
</cffunction>

<cffunction name = "doThis">
<cfscript>
var fantango = "";
fantango = variables.getclass.getYoYo();
</cfscript>
</cffunction>
</cfcomponent>

Jared Rypka-Hauer

unread,
Dec 2, 2009, 10:23:40 AM12/2/09
to cfc...@googlegroups.com
Move your createObject() call to within the init() method. The pseudo constructor carries a whole pile of issues with it (none of which I can exactly remember at the moment) because of the minute technical details of the way things are run at instantiation time.

Within init() is just generally considered the best practice and a good starting place to rule out any other weirdness.

J

On Nov 25, 2009, at 2:30 AM 11/25/09, DreamKnight wrote:

> ...
> Looks something like this:
> <cfcomponent>
> <cfscript>
> variables.getclass = createobject("component", "com.path.class").init
> ();
> </cfscript>
>
> <cffunction name="init" ...>
> <cfreturn this />
> </cffunction>
> ...

John Whish

unread,
Dec 2, 2009, 12:48:15 PM12/2/09
to cfc...@googlegroups.com
I believe that when you set variables outside of a method (including init), then it can only be hard coded values. If you call the createobject inside the init method then it will store a reference to your object which you can then call in other methods.

2009/12/2 Jared Rypka-Hauer <armcha...@gmail.com>

Bassil Karam

unread,
Dec 1, 2009, 8:16:09 PM12/1/09
to cfc...@googlegroups.com
So you get an error on this line?:

fantango = variables.getclass.getYoYo();

Whats the error?

Composing one object into another as you are doing is perfectly fine. 

Baz

Barry Beattie

unread,
Dec 2, 2009, 5:40:53 PM12/2/09
to cfc...@googlegroups.com
"when you set variables outside of a method (including init), then it
can only be hard coded values"

you're talking about the protected variables within the CFC, yes?

if so, not quite:

they can also be simple calculated values like "#now()#".

but the essence of what you're saying is true: by the time the code
passes that point, the value within those variables has to be known -
which includes <nothing> waiting for a method to supply it a value.
> --
>
> You received this message because you are subscribed to the Google Groups
> "CFCDev" group.
> To post to this group, send email to cfc...@googlegroups.com.
> To unsubscribe from this group, send email to
> cfcdev+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/cfcdev?hl=en.
>
Reply all
Reply to author
Forward
0 new messages