cooldude832 Posted July 28, 2008 Share Posted July 28, 2008 I don't know if I"m doing this right so let me explain first I have a class called stats . It is basically my page container for my statistics page so it has the primary key of the database item we're stating up and its owner's name etc I want to make a new extending class called graphs that can draw graphs. In the graphs class I want it to inherit all the data already set forth in a defined stats class (in php4) example <?php class stats{ var $var1 = "Bob"; } class graphs extends stats{ } $stats = new stats(); $stats->var1 = "George"; $graph_1 = new graphs(); #I want $graph-1->var1 to be George ?> make sense? Link to comment https://forums.phpfreaks.com/topic/116931-class-extention-inheriting-values/ Share on other sites More sharing options...
corbin Posted July 28, 2008 Share Posted July 28, 2008 Errr the entire point of objects (non static objects) is to have an instance of a class, but you're starting two instances... $graph = new graphs; $graph->var1 = "George"; Link to comment https://forums.phpfreaks.com/topic/116931-class-extention-inheriting-values/#findComment-601320 Share on other sites More sharing options...
stodge Posted July 28, 2008 Share Posted July 28, 2008 Deriving from graphs for a rendering class doesn't make sense. Create a GraphRenderer class, that accepts an instance of Graph and renders its data. Link to comment https://forums.phpfreaks.com/topic/116931-class-extention-inheriting-values/#findComment-601550 Share on other sites More sharing options...
cooldude832 Posted July 28, 2008 Author Share Posted July 28, 2008 The point of th extended class is because i want to generate multiple graphs but using different data sets that are still common the original stats object (like its primary key from mysql and other data) I can just important saying $graph->var1 = $stats->var1 I'll figure it out. Link to comment https://forums.phpfreaks.com/topic/116931-class-extention-inheriting-values/#findComment-601825 Share on other sites More sharing options...
mbeals Posted July 28, 2008 Share Posted July 28, 2008 so you have a stats class which has the data you wish to graph, and you want to be able to make many graphs from the data contained within stats? So you want to make the graph class an child of the stats class? extends lets you share common methods and attributes for the same instance. I would create a stats class (sounds like you have it already) and define a public $graphs = array(); attribute. Then create a graph Rendering class. If this class needs to be unique to the datatype, use the extends here. This might also be a good place for an abstract class. Now in the stats class, add methods to create new graph objects and assign their reference to the $graphs array. If you need different methods to handle the processing of the different stat types, then extend stats. Link to comment https://forums.phpfreaks.com/topic/116931-class-extention-inheriting-values/#findComment-602114 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.