RE: vars from parent class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Adrian,
In fact, I too use a global variable to hold the connection.  I changed the
code that I sent to get a new connection for each access to the database.
This is a bit wasteful and less efficient than re-using the connection to
satisfy the HTTP request.  So it would be better to assign a single
connection to a global variable and use that for all db access.

Does anyone have an opinion on the reuse of a single db connection for
multiple db accesses?  In Java and C# apps, using products like Weblogic,
Websphere, and in Tomcat, there are connection pooling facilities, which
allow a connection to be reused some configurable number of times before it
is deemed unreliable and is closed and a new one created.

So, yes, I think creating a connection and assigning it to a global variable
is a reasonable thing to do.

Cheers
Brian

-----Original Message-----
From: Adrian Ondrovic [mailto:ondrovic@xxxxxxxxxxxx] 
Sent: 17 March 2006 05:29
To: php-objects@xxxxxxxxxxxxxxx
Subject: RE:  vars from parent class

Thanks Brian a lot. I use similar class to handle mysql connection with a
few more functions handling basic mysql queries. The class is very useful.

I just encounter problems with passing parent class vars into a child class.

But you gave me a hint so i am gone try to use global vars.

Thanks a lot again Brian

Adrian 

-----Original Message-----
From: php-objects@xxxxxxxxxxxxxxx [mailto:php-objects@xxxxxxxxxxxxxxx]
On Behalf Of Brian Etheridge
Sent: Thursday, March 16, 2006 1:17 AM
To: 'php-objects@xxxxxxxxxxxxxxx'
Subject: RE:  vars from parent class

Hi Adrian,
I use a separate class specifically for the connection:

class Connection{

	var $linkId;

	function Connection(){
		$this->connection = $this->createConnection();
	}

	function getConnection(){ 
		
		$connection = new Connection();
		return $connection->linkId; 
	}

	function createConnection(){
		// Create a connecton to the db and save the link id.
		$this->linkId = mysql_connect(DB_HOST, DB_USER, DB_PWD);
		mysql_select_db(DB_NAME, $this->linkId);
	}
} 

This is used as follows:

	function load() {
		global $log;

		$query = "<sql string>";
		$result = mysql_query($query,
Connection::getConnection());
		if (mysql_errno() <= 0 && $result){ 
			if ($myrow = mysql_fetch_row($result)){
				$this->title = $myrow[0];
				$this->fileName = $myrow[1];
				// etc.
			}
			mysql_free_result($result);
		}  
		else{			 	
			$log->error("Error loading data, errno="
.mysql_errno() .", error=" .mysql_error());
		}
	}

In a configuration file somewhere you need to have:

$global_vars = array(		       
		       
// Database Vars
      "DB_HOST" => "<hostname>",
	"DB_NAME" => "<dbname>",
	"DB_USER" => "<username>",
	"DB_PWD" => "<password>",
	
	// Terminator, null, used so that we don't forget to have commas
above.
	"" => ""

);

// Globalize everything
while (list($key, $value) = each($global_vars)) {
  if ($key != "")
    define($key, $value);
}

All this code is taken out of context, so you may need to adjust it to get
it to work.

Brian

-----Original Message-----
From: Adrian Ondrovic [mailto:ondrovic@xxxxxxxxxxxx]
Sent: 16 March 2006 01:44
To: php-objects@xxxxxxxxxxxxxxx
Subject:  vars from parent class

Hi everyone,

1. Folks I have a parent class cMySQL the handles basic mysql queries and
connection ...

2. I have a child class cGetMenu which uses methods of parent class for
accessing mysql 3. in cGetMenu class the constructor checks if the given
menu table is of appropriate structure:

$fields = mysql_list_fields($this->MYSQL_DATABASE, $this->menu_table,
$this->connection);
		$columns = mysql_num_fields($fields);
		for ($i=0; $i<$columns; $i++):
		    $table_columns[] = mysql_field_name($fields, $i);
		endfor;

4. $this->MYSQL_DATABASE and $this->connection are the vars from existing
instance of parent cMySQL class

QUESTION: how do I get these vars from existing instant of parent class
MySQL and pass them to child?


My solution is by getting vars by get_object_vars() 

$sql = new cMySQL();
	$sql->MYSQL_PASSWORD="blablabla";
	$sql->MYSQL_USERNAME="blabla";
	$sql->MYSQL_DATABASE="dev";
	$sql->MYSQL_SERVER="localhost";
	$sql->connect();

$sql_vars = get_object_vars($sql);
$get_menu = new cGetMenu("menu_$lg", $sql_vars);

and then giving it to constructor of child class as an array and defining
them again

function cGetMenu($menu_table, $sql_vars){ // this function checks whether
there is a menu table or required structure // $sql_vars are variables from
existing instant of parent class cMySQL
		$this->menu_table = $menu_table;
		$this->MYSQL_DATABASE = $sql_vars['MYSQL_DATABASE'];
		$this->connection = $sql_vars['connection']; .....


Is there any other way how to solve this?

Thanks, thanks, thanks

Adrian 






PHP Data object relational mapping generator http://www.metastorage.net/
Yahoo! Groups Links



 




PHP Data object relational mapping generator
http://www.metastorage.net/ 
Yahoo! Groups Links



 




PHP Data object relational mapping generator
http://www.metastorage.net/ 
Yahoo! Groups Links



 



PHP Data object relational mapping generator
http://www.metastorage.net/ 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-objects/

<*> To unsubscribe from this group, send an email to:
    php-objects-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux