Copy link to clipboard
Copied
I will try to make this as short as possible. I have a component that looks like this:
<cfcomponent extends = "path.to.base" output = "false">
<cfproperty type="string" name="ID" />
<cfset variables.my = {} />
<cfset variables.my.ID = '' />
<cffunction access="public" returntype="path.to.component" name="init" output="false">
<cfargument required="false" type="string" name="ID" default="" />
<cfset setProperty( 'ID', arguments.ID ) />
<cfreturn this />
</cffunction>
</cfcomponent>
The setProperty() method exists in the extended 'path.to.base' component. It looks like this:
<cffunction access="public" returntype="boolean" name="setProperty" output="false">
<cfargument required="true" type="string" name="propertyName" />
<cfargument required="true" type="any" name="propertyValue" />
<cfset variables.my[ arguments.propertyName ] = arguments.propertyValue />
<cfreturn true />
</cffunction>
I am using an onMissingMethod() in my path.to.base component which allows me to call properties like myObj.ID() (and since the ID method does not exist, it checks if 'ID' exists in variables.my, and if so, returns it. So, I have code like this:
<cfset myObj = new path.to.component( '12345' ) />
<cfdump var="#myObj.ID()#" />
<cfset myObj.setProperty( 'ID', '54321' ) />
<cfdump var="#myObj.ID()#" />
<cfdump var="#myObj#" />
Line 2 outputs: '12345' (expected)
Line 4 outputs: '54321' (expected)
Line 5 dumps the object, and it has the methods exepcted, and a PROPERTIES substructure, with an ID key, but the value is empty.
So my question is "Why is ColdFusion not updating the respective Property, even though it is definitely updating in private variables.my keys?
Copy link to clipboard
Copied
To start with, I think that it is not good practice to set or update a component's instance variables by means of arbitrary methods as you have done. Use a 'setter' method to set instance variables. That way, if you get an empty string during debugging, you will know the setter method had not been called. You should also "var" your methods' variables. However, the issue you report is caused by something else.
ColdFusion stores methods as variables, too. In my opinion, when you give the same name to a property and to a method, you place ColdFusion in an ambiguous situation. So change the name of the method in the path.to.base component from 'ID' to 'getID'.
Copy link to clipboard
Copied
Previous to what I've posted up here, I was using more purist getter/setter methodologies. There were pros and cons to each side, and though many purists don't budge an ounce in their views, I saw a lot of value in writing code like:
<!--- Get the ID. --->
<cfset myID = objectName.ID() />
<!--- Set the ID. --->
<cfset objectName.setProperty( 'ID', 12345 ) />
There was a lot of readability to this method, even though it was not as traditional as code like:
<cfset myID = objectName.getID() />
<cfset objectName.setID( '12345' ) />
The former really allowed for the removal of a lot of code, since I could remove all getProperty() getters (since it used onMissingMethod() when non-existing methods like obj.ID() are called, as well as removing a lot of setter methods, since 1 global setProperty() method could, again, be used to set properties across the board.
I understand there is pro and con to this, it breaks encapsulation, does not allow finite access control, etc. But I've been mulling over whether I want to be an OOP purist vs. realist.
Copy link to clipboard
Copied
Here, as in every other profession, rules are meant to be broken! The key issue here is not whether you use getters and setters. It is that ColdFusion may be confusing the name 'ID' of the function with that of the property. I think that the problem you have encountered will be solved if you change the name of the function to something else. That is, to anything other than 'ID', not necessarily to 'getID'.
Copy link to clipboard
Copied
Actually makes a lot of sense. I mean, heck, I'm not not following the non-purist methodologies to be considered a pain in the butt n all but it does come with its own side of cons as well as pros. The fact that I use the onMissingMethod() and omit the getProperty() might be the reason why we don't see the reflection, however the dump of that component's variables scope shows that they are at least being updated accordingly.
Good muse, mi amigo. Appreciate it.