It may not be perfect for every scenario but it works fairly well for what I need:
<cffunction name="copy" access="public" output="false">
<cfscript>
var params = getMetaData(this.init).parameters;
var i = 0;
var thisKey = "";
var args = structNew();
for(i=1;i LTE ArrayLen(params); i=i+1){
thisKey = params[i].name;
args[thisKey] = this[thisKey];
}
return createObject("component",getMetaData(this).name).init(argumentCollection = args);
</cfscript>
</cffunction>
Now, there are some limitations already, especially concerning properties that are objects. currently they aren't copied, they are just pointed at by reference. I will probably keep working on this till I resolve that issue (handling recursive copying).
But, for the time being this is a decent start.
well it turns out i was able to answer my own question:
<cffunction name="copy" access="public" output="false">
<cfreturn createObject("component",getMetaData(this).name).init(argumentCollection=variables.instance) />
</cffunction>
here is a link that tries to do more, and where I found out about the getmetadata method - however, the example code on the following page doesn't work as near as I can tell:
http://www.prismix.com/blog/archives/2004/08/coldfusion_cfc.cfm
becuase I couldn't reference the properties value of getMetaData(this)
If I figure out a way to actually reference the properties of an object as well I'll let you know.
BillOn Apr 1, 2005 2:53 PM, Jim Davis <[EMAIL PROTECTED]> wrote:----------------------------------------------------------I've been meaning to look into a generic clone method myself so this interests me as well.
As for getting the object type that's pretty simple using getMetaData() – but I think a true clone method will be more difficult than that.
Jim Davis
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] On Behalf Of Bill Rawlinson
Sent: Friday, April 01, 2005 2:26 PM
To: [email protected]
Subject: [CFCDev] dynamically identifying an objects TYPE
Is there a way in CF to identify an objects type on the fly.I have 47! gmail invites,want one? ----------------------------------------------------------
for instance, say I have a generic object that has some objects inheirited from it.
In the generic object I would like to put a generic "copy" method that would work sort of like this:
<cffunction name="copy" output="false" returntype="any" />
<cfset var objectType = this.type />
<cfreturn createObject("component",objectType).init(argumentCollection = variables.instance)) />
</cffunction>
this probably isn't possible, but I figured I would ask. It would be much cleaner to have this simple copy operation in the base class than having to basically duplicate it in each inheirited class.
thanks
Bill
--
[EMAIL PROTECTED]
http://blog.rawlinson.us
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]
--
[EMAIL PROTECTED]
http://blog.rawlinson.us
I have 47! gmail invites,want one?
--
[EMAIL PROTECTED]
http://blog.rawlinson.us
I have 47! gmail invites,want one?(¹ªÞ²æìr¸›yÛhq÷zôèº{.nÇ+‰·¬zwZ?隊[hq÷z÷s:'zŠàÂ+a¶°¢·lº{.nÇ+‰·œ}Ç^½«-…ë.n7œ¶‡í…ç¦j)ADB Þ¾++ºvòP™¢w°Ãs:'zŠàjwlºšh®×?o …\z,¶)àÃh²Ø§?Ê&Qv«r¯z‡í…à…7¯–+-ŠÆ¯j)ZnWš´pà šŠV«r¯yÊ&ýÇÜuëÜ}Ìè?ê+
