> >>I would suggest something like this: > >> > >>class myotherclass { > >> var $db > >> > >> function myotherclass(&$db){ > >> $this->db =& $db; > >> > >> $sql="select * from mytable"; > >> > >> $this->db->dosql($sql); > >> } > >>} > >> > >>$db = new mydbclass(); > >>$other = new myotherclass($db); > >> > >>I would also suggest capitalizing the class names and using another DB > >>abstraction layer (such as PEAR DB or MDB) but that's your option. ;-) > > > > > > Good point Justin. You do not necessarily need a $db var in your data > > class - just use the passed $db object in your method: > > > > class myotherclass { > > var $db; > > > > function myotherclass(&$db) { > > > > $sql = 'select * from mytable'; > > $db->dosql($sql); > > } > > } > > > > Another way is to declare the $db variable global in your methods so that it > > is available without the need to pass it to *every* single method: > > Global vars are generally a bad idea... It leads to spaghetti code and > can end up giving you huge headaches if you're not very careful. > > Also, I am not saying that $db should be passed into every method. It > should be passed into the constructor and assigned as a class variable > (as my code above shows). Then, any method in the object can get to it.
You're right, Justin. What you suggested seems to be the best way. I'll be adding var $db to my Base class and pass the db object to the constructor. Thanks and regards, Torsten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php