Jump to content

[SOLVED] using 1 connection among many classes


shakeitdown

Recommended Posts

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'); 
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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];
   }
}

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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 
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.