shakeitdown Posted September 22, 2007 Share Posted September 22, 2007 let's look at it the code below. it is a basic dynamic instantiation class. to me, it only makes sense to connect the WORLD class to a mysql connection. the problem is that you want CITY to be able to query the database using the connection created in WORLD. likewise, you might want COUNTRY to be able to query the same connection... thanks for your help! <?php class CITY { private $population; public function __construct($cityname) { // PROBLEM:Load some city-specific data with mysql query. // this should use $mysqlconn from the WORLD class. i would really prefer // not to pass it through during the instantiation and i'm thinking there's a //way! } public function population($demographic = 'all') { return $this->population[$demographic]; } } class COUNTRY { private $code = null; private $cities = array(); public function __construct($code) { $this->code = $code; } public function city($cityname) { if (!$this->cities[$cityname]) { $this->cities[$cityname] = new CITY($cityname); } return $this->cities[$cityname]; } } class WORLD { private $countries = array(); private $mysqlconn = new MYSQL_CONN($user, $pass, $db, $server); public function country($code = 'us') { if (!$this->countries[$code]) { $this->countries[$code] = new COUNTRY($code); } return $this->countries[$code]; } } class MYSQL_CONN { //you can imagine what goes here... } $world = new WORLD; // performs mysql query to load boston information such as square miles, mayor, population $world->country('us')->city('boston'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/70273-solved-using-1-connection-among-many-classes/ Share on other sites More sharing options...
emehrkay Posted September 23, 2007 Share Posted September 23, 2007 you can approach this a few ways; Make the WORD class connect to the db and make any other class extend WORLD. In the children's constructors, do a parent::_construct() to recreate the connection. You could also do the extensions, but use a singleton patten to ensure that each call to the database is using the same resource. For instance, on one page of your script, you might use the CITY class and then the COUNTRY class. Both extends WORLD which holds the connection. If it wasnt set up to be a singleton, there would be multiple instances of WORLD running. http://www.google.com/search?q=php+singleton&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a Quote Link to comment https://forums.phpfreaks.com/topic/70273-solved-using-1-connection-among-many-classes/#findComment-353125 Share on other sites More sharing options...
shakeitdown Posted September 23, 2007 Author Share Posted September 23, 2007 yea, i agree with your theory but i cannot for the life of me figure out how to implement it. i've tried everything possible and just can't get it to work! for ex: class COUNTRY extends WORLD { private $code = null; private $cities = array(); public $connection; public function __construct($code) { $this->code = $code; $this->connection = parent::getConnection(); //RETURNS NULL THOUGH I THINK IT SHOULDN'T $this->connection = WORLD::getConnection(); //RETURNS NULL AS WELL } public function city($cityname) { if (!$this->cities[$cityname]) { $this->cities[$cityname] = new CITY($cityname); } return $this->cities[$cityname]; } } class WORLD { private $countries = array(); public $mysqlconn; public function __construct() { $mysqlconn = new MYSQL_CONN($user, $pass, $db, $server); var_dump($this->mysqlconn); // SURPRISINGLY THIS RETURNS VALUES!!! } public function getConnection() { var_dump($this->mysqlconn); // SURPRISINGLY THIS RETURNS NULL!!! return $this->mysqlconn; } public function country($code = 'us') { if (!$this->countries[$code]) { $this->countries[$code] = new COUNTRY($code); } return $this->countries[$code]; } } Quote Link to comment https://forums.phpfreaks.com/topic/70273-solved-using-1-connection-among-many-classes/#findComment-353135 Share on other sites More sharing options...
shakeitdown Posted September 23, 2007 Author Share Posted September 23, 2007 in the above example, it seems that the getConnection() function returns NULL only because it's being called from a child class which doesn't have access to the $this->mysqlconn variable....ARG! Quote Link to comment https://forums.phpfreaks.com/topic/70273-solved-using-1-connection-among-many-classes/#findComment-353150 Share on other sites More sharing options...
emehrkay Posted September 23, 2007 Share Posted September 23, 2007 did you look into the singleton design pattern? http://en.wikipedia.org/wiki/Singleton_pattern#PHP_5 Quote Link to comment https://forums.phpfreaks.com/topic/70273-solved-using-1-connection-among-many-classes/#findComment-353202 Share on other sites More sharing options...
shakeitdown Posted September 23, 2007 Author Share Posted September 23, 2007 thanks for the link. i looked at it, but alas still don't understand exactly. i guess i'm going to make the MYSQL class a singleton style class...will play with that for a while. there are some weird things though...for example, because the MYSQL class needs a $db, $server, $user, $pass how can it be dynamically created? do you have to hard code this into the singleton? i thought i was going to have to do all sorts of crazy &= references and @ stuff, which was just beyond me... Quote Link to comment https://forums.phpfreaks.com/topic/70273-solved-using-1-connection-among-many-classes/#findComment-353269 Share on other sites More sharing options...
emehrkay Posted September 23, 2007 Share Posted September 23, 2007 quick example, this is almost exactly how i use the singleton for my db access <?php class DB{ public static $_instance; private $_db_user = 'asdafs'; private $_db_psss = 'asdfasdf'; private function __construct(){ //make the db connection here } public static function getInstance() { if (self::$instance === null) { self::$instance = new DB(); } return self::$instance; } private function runQuery($query){ //run query code } } class COUNTRY{ private $_DB; public function __construct(){ $this->_DB = DB::getInstance(); } public function getAll(){ $q = "SELECT * FROM country"; $this->_DB->runQuery($q); } } class STATE{ private $_DB; public function __construct(){ $this->_DB = DB::getInstance(); } public function getAll(){ $q = "SELECT * FROM state"; $this->_DB->runQuery($q); } } $country = new COUNTRY(); $state = new STATE(); $country->getAll(); $state->getAll(); //they both use the same db connection ?> Quote Link to comment https://forums.phpfreaks.com/topic/70273-solved-using-1-connection-among-many-classes/#findComment-353404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.