RE: vars from parent class

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

 



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

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