Skip to main content
Inspiring
August 29, 2008
Question

pseudo constructor

  • August 29, 2008
  • 9 replies
  • 1014 views
I am going through a petmarket application can't remember which one it was I have it printed out but I am using it to finally understand cfcs. In this short bit on the component before the methods we have what I know to be the the "pseudo constructor". A couple of questions please about why the author did these two things:
Why are we setting the cfproperty values?
Why are we setting defaults for everything in this scope-surely we want to use arguments instead with required false?
I have seen this a lot cfsetting var = createobject() why would you do this to invoke in the case in the code the component for all the methods in the cfc?
Thank you
This topic has been closed for replies.

9 replies

BKBK
Community Expert
Community Expert
September 2, 2008
The use of the this scope here is bad, it's abundance, bad, bad, bad.

Inspiring
September 1, 2008
The way I use init functions is something like this.

<cfcomponent>
<cfset SomeGlobalVariable = "">

<cffunction name="init" returnvariable="boolean">
<cfargument name="Something">
<cfset SomeGlobalVariable = arguments.something>


<cffunction name="NotInit">
<cfargument name="Something">
<cfscript>
var x = false;
if (variables.SomeGlobalVariable is "")
x = init(argumentcollection = arguments);

To call it, you can use cfobject or createobject to start. Then you can either call the init method (which I would do) and the other functions, or you can simply call the other functions, and the init function would be called from within the cfc.
Inspiring
August 30, 2008
Thanks for the replies (Adam I just read yours after I posted mine so apologies you answered that cfparam one that Ian kindly elaborated on).
So if I understand right then this line would be got rid of, replaced with the an init function/method -how would that be referenced?
<cfset this.cart =createobject("component", "petmarket.shoppingcart")>

Is the cfset with createobject totally out of date then?
Thank-you
Inspiring
August 29, 2008
Hydrowizard wrote:
> thanks for those replies so why does the author cfparam rather than cfarfgument?

You can nott use <cfargument...> in the pseudo constructor outside of
<cffunction...></cfunction> blocks. There is not way to pass arguments
to the constructor of a CFC which is a big reason the community best
practice is to use an function named init as an alternate constructor.


Why <cfparam...> over <cfset...> which would accomplish the exact same
thing, I have no idea. Possibly just habit from older ColdFusion
reusable code concepts such as custom tags.

That is a very old CFC|Flex integration example and looks to be rather
outdated.
Inspiring
August 29, 2008
thanks for those replies so why does the author cfparam rather than cfarfgument?
Inspiring
August 29, 2008
Looking at that code, I suspect you should avoid any guidance suggested by
the author, as they don't really seem to know what they're doing.

Ian explained the <cfproperty> tags, and I'm pretty sure the example code
is misusing them.

All the <cfparam> stuff is rubbish for a couple of reasons:
- there's no way the values can *possibly* exist before the object is
created, so they should just be <cfsets>.
- one would not want all that stuff exposed in the THIS scope. It should
be variables-scoped.

Oh, a third reason:
- accepted practice is to have an init() method to set this sort of thing;
avoiding the pseudo-constructor stuff.


On the basis of what you've presented, I feel if you follow the guidance of
this tutorial, you will end up learning bad practices.

That said, I don't know what else to suggest: I have never used any CF
tutorials at any stage, unfortunately.

--
Adam
Inspiring
August 29, 2008
Inspiring
August 29, 2008
It is my understanding that the <cfproperty...> tag is useful for
components designed to be used as a web services and|or with Flex front
ends through LifeCycle nee Flex Data Services.

The help with the WSDL generation or some such function.

From the documentation

<quote src=" http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-b18.htm">
cfproperty

Defines properties of a ColdFusion component (CFC). Used to create
complex data types for web services. The attributes of this tag are
exposed as component metadata and are subject to inheritance rules.
</quote>
Inspiring
August 29, 2008
quote:

Originally posted by: Hydrowizard
I am going through a petmarket application can't remember which one it was I have it printed out but I am using it to finally understand cfcs. In this short bit on the component before the methods we have what I know to be the the "pseudo constructor". A couple of questions please about why the author did these two things:
Why are we setting the cfproperty values?
Why are we setting defaults for everything in this scope-surely we want to use arguments instead with required false?
I have seen this a lot cfsetting var = createobject() why would you do this to invoke in the case in the code the component for all the methods in the cfc?
Thank you


I can answer part of it.

Setting properties is one way to create variables that are available to all functions in the cfc. It's what I did when I first started writing them. It might not be the best method, but it works. Your code sample does not show these property values being set. I use an init function to do that.

To the best of my knowlege, the property scope and the this.scope are different. If that's correct, your sample has some redundant code.

WRT cfparaming the "this" scope variables, you have to look at the rest of the cfc to see how they are being used. You'll also have to look at functions that accept arguments and see what's happening with those variables.

WRT to creating objects instead of invoking, if you are going to invoke more than one method of the cfc, it is more efficient to create an object and call the functions directly than it is to use cfinvoke. The reason is that cfinvoke essentially creates the object, calls the method, and drops the object.