Inherited Properties in a CFC

39 views
Skip to first unread message

JEAguilar

unread,
Jul 30, 2014, 8:17:40 AM7/30/14
to ra...@googlegroups.com
Hi,

I'm not sure if this is a bug or just my misunderstanding of how CFC inheritance should work. Assume the following three files:

Parent.cfc

component displayname="Parent" accessors="true" {
 property name
="ID" type="numeric" access="private";
 property name
="Name" type="string";
}

Child.cfc

component displayname="Child" accessors="true" extends="Parent" {
 property name
="ChildID" type="numeric";
 property name
="ChildName" type="string";
}

test.cfm

<cfscript>
CHILD = new Child();

CHILD.setID(123);
CHILD.setName("ABC");
CHILD.setChildId(987);
CHILD.setChildName("ZYX");

echo( CHILD.ID & " " & CHILD.Name );
echo( serializeJSON(CHILD) );
</cfscript>

I can verify that the inherited ID and Name are being set. And the accessors are being inherited as well. The output of test.cfm is:

123 ABC
{"ChildID":987,"ChildName":"ZYX"}

I would expect the output to be:

123 ABC
{"ChildID":987,"ChildName":"ZYX","ID":123,"Name":"ABC"}

If I dump(CHILD), I get the following:

Component (Child) Child
Only the functions and data members that are accessible from your location are displayed
ExtendsParent
Properties
ChildID
number987
ChildName
stringZYX
public
getChildID
Public Function getChildID (generated)
source:/Users/juan/Sites/Child.cfc
arguments
labelnamerequiredtypedefaulthint
return typenumeric
setChildName
Public Function setChildName (generated)
source:/Users/juan/Sites/Child.cfc
arguments
labelnamerequiredtypedefaulthint
ChildNametruestringnull
return typeany
getChildName
Public Function getChildName (generated)
source:/Users/juan/Sites/Child.cfc
arguments
labelnamerequiredtypedefaulthint
return typestring
getName
Public Function getName (generated)
source:/Users/juan/Sites/Child.cfc
arguments
labelnamerequiredtypedefaulthint
return typestring
setChildID
Public Function setChildID (generated)
source:/Users/juan/Sites/Child.cfc
arguments
labelnamerequiredtypedefaulthint
ChildIDtruenumericnull
return typeany
setName
Public Function setName (generated)
source:/Users/juan/Sites/Child.cfc
arguments
labelnamerequiredtypedefaulthint
Nametruestringnull
return typeany
setID
Public Function setID (generated)
source:/Users/juan/Sites/Child.cfc
arguments
labelnamerequiredtypedefaulthint
IDtruenumericnull
return typeany
getID
Public Function getID (generated)
source:/Users/juan/Sites/Child.cfc
arguments
labelnamerequiredtypedefaulthint
return typenumeric

with the Parent properties missing. So, am I confused, missing something blindingly obvious, or is this a bug?

Thanks,

Juan

Chris Blackwell

unread,
Jul 30, 2014, 8:49:22 AM7/30/14
to railo
I don't think you are misunderstanding how inheritance works.  IMHO, this is a bug in serializeJson().
You can fix it by including a _toJson() function in your parent cfc that properly serializes all inherited properties.

Here's a basic example that assumes all your properties have a public getter function

function _toJSON() {
var data = {};
var meta = getMetaData(this);
var props = [];
while(structKeyExists(meta, 'extends')) {
if(structKeyExists(meta, 'properties')) {
for(var p in meta.properties) {
props.append(p);
}
}
meta = meta.extends;
}

for(var p in props) {
data[p.Name] = this["get#p.Name#"]();
}

return serializeJSON(data);
}


--
Did you find this reply useful? Help the Railo community and add it to the Railo Server wiki at https://github.com/getrailo/railo/wiki
---
You received this message because you are subscribed to the Google Groups "Railo" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/railo/7189b45d-0060-4c04-8020-b5c591437aa6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jared Rypka-Hauer

unread,
Jul 31, 2014, 11:38:56 AM7/31/14
to ra...@googlegroups.com
If you look at the dump of CHILD, you have the top cell that says Component (Child) Child.

Just below that you have:

Extends: Parent

That should be clickable… if you click on it it should expand to so you the parent’s methods and properties as well.

As for your serializeJSON(), You’d be better off to use a method on the parent object that returns a JSON string representing the fields of the object from the parent and child. You don’t want to be breaking encapsulation by having external code directly accessing the contents of the object anyway. :)

On the other hand… it does seem like a bug… since IS-A is how we describe the parent-child relationships, you’d think the child object would present all the fields of both itself and the parent combined. If serializeJSON() will allow you to see the properties of the child it should allow you to see the properties of the whole inheritance chain…

Something to think about, anyway...

J

On Jul 30, 2014, at 7:17 AM, JEAguilar <jeag...@gmail.com> wrote:

Hi,

I'm not sure if this is a bug or just my misunderstanding of how CFC inheritance should work. Assume the following three files:

Parent.cfc

component displayname="Parent" accessors="true" {
 property name="ID" type="numeric" access="private";
 property name="Name" type="string";
}

Child.cfc

component displayname="Child" accessors="true" extends="Parent" {
 property name="ChildID" type="numeric";
 property name="ChildName" type="string";
}
...
Reply all
Reply to author
Forward
0 new messages