slogger2 Posted February 1, 2009 Share Posted February 1, 2009 Is it possible to merge a property in a child object with a property in its parent object? I will give an example to describe: class A { var $something; function show() { foreach($this->stuff as $thing) { echo $thing . " "; } } } class B extends A { var $something = array(1,2,3); } class C extends B { var $something = array(4,5,6); } $myVar = new C(); $myVar->show(); // Desired output: 1 2 3 4 5 6 Essentially, when I set the value of a property in a child class, I want it to be merged with the same property in its parent class. I'm not sure if this is possible, but I think it would be because in CakePHP, some properties (such as the $uses variable in a controller) is merged with that of its parent (namely AppController). I would presume that this requires either some control functionality in either the base class (in this case, class A), or the code which runs the class. Thanks a bundle in advance... Link to comment https://forums.phpfreaks.com/topic/143376-solved-merging-properties/ Share on other sites More sharing options...
slogger2 Posted February 1, 2009 Author Share Posted February 1, 2009 Never mind, I have figured out how to do what I described. A simple merge function in the parent and some looping of get_parent_class() and get_class_vars() seems to do the trick. If anyone is interested in the complete solution, I can paste it here. Link to comment https://forums.phpfreaks.com/topic/143376-solved-merging-properties/#findComment-752045 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.