NYCPHP Meetup

NYPHP.org

[nycphp-talk] OO Inheritance concept and problem

Phil Powell phillip.powell at adnet-sys.com
Thu Feb 19 11:02:41 EST 2004


<?php

/*------------------------------------------------------------------------------------------------------------------------------------
	File: DB.INC.PHP
	Author: UNKNOWN
	Created: ??
	Modified: 2/18/2004 BY PHIL POWELL
	Purpose: Controller classes for database connections, disconnections, queries and resultsets
	Dependencies: NONE
	Cookies: NONE
	Sessions: NONE
	Privacy Scope: Unviewable
   ------------------------------------------------------------------------------------------------------------------------------------*/

class dbConnection {

	/*----------------------------------------------------------------------------------------------------------------
		Legacy class that establishes database connection and disconnection
	------------------------------------------------------------------------------------------------------------------*/

	var $dbHost;			// PROPERTY CONTAINING DB HOST
	var $dbPort;			// PROPERTY CONTAINING DB PORT
	var $dbUser;			// PROPERTY CONTAINING DB USERNAME
	var $dbPwd;			// PROPERTY CONTAINING DB PASSWORD
	var $dbName;		// PROPERTY CONTAINING DEFAULT DB NAME (FOR mysql_select_db() MANDATORY COMMAND FOR QUERYING IN PHP)


	function dbconnection($dbHost,$dbPort,$dbUser,$dbPwd,$dbName) {		 // CONSTRUCTOR
		foreach (array('dbHost', 'dbPort', 'dbUser', 'dbPwd', 'dbName') as $val) $this->$val = ${$val};
		$this->dbServer = $this->dbHost . ":" . $this->dbPort;
	}

	

	function connect() {															// RESOURCE LINK (OR NULL) METHOD
		$dbcnx = @mysql_connect($this->dbServer, $this->dbUser, $this->dbPwd);
		if (!$dbcnx) {
                 return false;
		} else {
		 $dbselect = mysql_select_db($this->dbName, $dbcnx);
		}

		if (!$dbselect) {
		 return false;
		} else {
		 return $dbcnx;
		}
	}

	function close() {																// BOOLEAN METHOD
		if (@!mysql_close($this->connect())) return false;
                return true;
	}

}


class MySQLQuery {

	/*------------------------------------------------------------------------------------------------------------------------------------
		This legacy class will perform queries and return results if applicable, also free results and returns affected
		rows also if applicable
	--------------------------------------------------------------------------------------------------------------------------------------*/

	var $sql;			// SQL STRING PROPERTY
	var $mySQLConn; 	// MySQLQuery CLASS OBJECT RESOURCE LINK PROPERTY	

	function MySQLQuery($sql, $mySQLConn) {				// CONSTRUCTOR
		$this->sql = $sql;
		$this->mySQLConn = $mySQLConn;
	}

	//------------------------------------------ --* GETTER/SETTER METHODS *-- --------------------------------------


	function getResult() {										// ARRAY-OF-OBJECT METHOD
		$runquery = $this->runQuery();
		$count = 0;
		while ($row = mysql_fetch_object($runquery)) {
			$result[$count] = $row;
			$count++;
		}
		return $result;
	}
	

	function getRows($what = 'select') {						// ARRAY METHOD
		$runquery = $this->runQuery();
		$count = 0;

		if (strcmp(strtolower($what), 'affected') == 0) {
		 $row = @mysql_affected_rows($this->mySQLConn);
                } else {
		 $row = @mysql_num_rows($this->mySQLConn);
                }

		return $row;
	}
	//-------------------------------------------- --* END OF GETTER/SETTER METHODS *-- ------------------------------



	function freeResult() {									// VOID METHOD
		@mysql_free_result($this->runQuery());
	}

	function runQuery() {										// RESULT RESOURCE LINK METHOD
		$result = @mysql_query($this->sql, $this->mySQLConn);
		if (mysql_errno()) trigger_error(mysql_error() . ' using query: ' . $this->sql, E_USER_ERROR);
		return $result;
	}

}

class MethodGeneratorForActionPerformer {


	var $isSuccessful = true;						// BOOLEAN PROPERTY DETERMINING IF ACTION WAS SUCCESSFUL
	var $hasDuplicateUniqueKey = false;					// BOOLEAN PROPERTY DETERMINING UNIQUENESS
	var $errorArray = array();						// ASSOCIATIVE ARRAY PROPERTY FIELD => ERROR MSG
	var $tableName;							// DB TABLE NAME PROPERTY
	var $fileName;							// FILE NAME PROPERTY
	var $dbConn;							// DB CONNECTION OBJECT
	var $dbConnObj;							// DB RESOURCE LINK OBJECT


	function MethodGeneratorForActionPerformer() {}		//  CONSTRUCTOR

	//----------------------------------------------------* GETTER/SETTER METHODS *--------------------------------------------------------------

	function getDBConn() {			// RESOURCE LINK METHOD
		return $this->dbConn;
	}

}


class DBActionPerformer extends MethodGeneratorForActionPerformer {

	var $id;

	function DBActionPerformer($id = '') {		// CONSTRUCTOR
		$this->id = $id;
	}
//---------------------------------------------* DB ACTION METHODS *----------------------------------------------

	function connect() {								// VOID METHOD
		global $dbHost, $dbPort, $dbUser, $dbPwd, $dbDefaultName, $action;
		$this->dbConnObj =& new dbConnection($dbHost, $dbPort, $dbUser, $dbPwd, $dbDefaultName);
  		$this->dbConn = $this->dbConnObj->connect();
		if (!$this->dbConn) {
		 $this->isSuccessful = false;
		 $this->setErrorArray(array($action => 'Could not connect to the database'));
		}
	}

	function disconnect() {							// VOID METHOD
		if ($this->dbConn) $this->dbConnObj->close();
		$this->dbConnObj = null;
		$this->dbConn = null;
	}

}


class ActionPerformer extends DBActionPerformer {

	function ActionPerformer($id = '') {		// CONSTRUCTOR
		$this->id = $id;
	}

	function add_dept() {						// VOID METHOD
		$this->connect();
		$sql = "SELECT * FROM blah";
		$query =& new MySQLQuery($sql, $this->getDBConn());
		$result = $query->getResult();
		$result = null;
		$sql = "INSERT INTO blah (name) VALUES ('foo')";
		$query =& new MySQLQuery($sql, $this->getDBConn());
		if (!$query->runQuery()) $this->setErrorArray(array($action => 'Could not perform insert'));
		$query = null;
		$this->disconnect();
	}

}

$actionPerformer =& new ActionPerformer($id);
$actionPerformer->add_dept();
$actionPerformer = null;
		
?>

I built this chain of classes (which normally reside in different .inc.php files due to scope) to handle specific database connections and actions.  

However, I am noticing that were I to replace this line in ActionPerformer::add_dept():

$result = $query->getResult();

With:

$result =& $query->getResult();

All subsequent instantiations of any MySQLQuery objects result in wacky parameter assignments. 

For example, were I to do a print_r after the second line of:
$query =& new MySQLQuery($sql, $this->getDBConn());

The output becomes:

mysqlquery Object ( [sql] => INSERT INTO blah (name) VALUES ('foo') [mySQLConn] => Resource id #19 )

That's totaly ok.. but if I use this line instead: $result =& $query->getResult(); and then do the same actions below this is what I get:

mysqlquery Object ( [sql] => Resource id #19 [mySQLConn] => Resource id #19 )


So my question is this: When should I be using the reference pointer '&' for object instantiation and why?  Based on what you see so far,
hopefully you're able to figure this out.

Thanx
Phil



-- 
Phil Powell
Web Developer
  ADNET Systems, Inc.
  11260 Roger Bacon Drive, Suite 403
  Reston, VA  20190-5203
Phone: (703) 709-7218 x107   Cell: (571) 437-4430   FAX: (703) 709-7219
EMail:  Phillip.Powell at adnet-sys.com      AOL IM: SOA Dude







More information about the talk mailing list