Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass Instance of Class in Function Call

Status
Not open for further replies.

MikeBronner

Programmer
May 9, 2001
756
US
I must be having a brain-fart or something. Something I know I have done in the past is giving me problems today and I can't get my head around it. (Must be one of those days ...)

Here's my situation: I want to pass the instance of a class (my db abstraction class) into another class, like so:
Code:
$myClass->set_database($r_database);

Now, in myClass I consume it like so:
Code:
class myClass
{
    private $r_db = null;
    [...]
    public function set_database($r_db)
    {
        $this->$r_db = $r_db;
    }
    [...]
}

In the set-method it gives me the following error:
PHP Notice: Object of class Database to string conversion in myClass.php on line 38
Where line 38 is
Code:
$this->$r_db = $r_db;

I'm stumped! Anyone know a way out? Oh, and if I do this in PHP 5.2.0 it returns the new error:
PHP Catchable fatal error: Object of class Database could not be converted to string in myClass.php on line 38

Take Care,
Mike
 
I tested with 5.1.6 then upgraded to 5.2.0.

Neither version displayed the error. Here's my test code:

Code:
<?php
class Database
{
        var $foo;

        function Database ()
        {
                $this->foo = 3;
        }
}

class myClass
{
        private $r_db = null;

        function myClass ()
        {
                $this->r_db = '';
        }

        public function set_database($r_db)
        {
                $this->r_db = $r_db;
        }
}

$a = new Database();

$myClass = new myClass();

$myClass->set_database($a);
?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
Hmm, thanks for trying that. Of course the code I posted is extremely simplified, so I wonder if there's something else than the passing that is causing this.

Take Care,
Mike
 
Nearly certainly. The operative question is, "Why would PHP find it necessary to try to convert the Database class to a string?"



Want the best answers? Ask the best questions! TANSTAAFL!
 
Well, here's the code to my database class. I can't see anything out of the ordinary. Mayhap a second pair of eyes helps here:
Code:
	class Database
	{
/**
* @access private
* @var string Username used to access the database.
*/
		private	$_s_username	= '';
/**
* @access private
* @var string Password used to access the database.
*/
		private	$_s_password	= '';
/**
* @access private
* @var string Identifies database to be used.
*/
		private	$_s_database	= '';
/**
* @access private
* @var string Identifies server to connect to.
*/
		private	$_s_server		= '';
/**
* @access private
* @var string Database type. Default is mssql.
*/
		private $_s_type		= '';
/**
* @access private
* @var resource Contains connection resource for database.
*/
		private $_r_connection	= null;

/**
* Constructor method that will default the database connection to host
* to either csdbbcdev001 or csdbbmsql001 (development or production)
* if any of the parameters are left blank (excluding type).
*
* @access public
* @todo Complete exception handling functionality in try-catch blocks.
* @param string $username Sting that determines the LDAP server to connect to.
* @param string $password Sting that determines the LDAP server to connect to.
* @param string $database Sting that determines the LDAP server to connect to.
* @param string $server Sting that determines the LDAP server to connect to.
* @param string $type Determines database type. Can be fbsql, ibase, mssql, mysql, mysqli, oci8, pgsql, querysim, or sqlite.
* @author Mike Bronner <michael.g.bronner@kp.org>
* @version 1
*/
		public function __construct($username = null, $password = null, $database = null, $server = null, $type = null)
		{
			try
			{
				if ((strlen($username) == 0)
					|| (strlen($password) == 0)
					|| (strlen($database) == 0)
					|| (strlen($server) == 0))
				{
					$this->_s_username = 'web';
					$this->_s_password = 'web';
					$this->_s_database = 'case_coordination_center';
					if (strtolower($_SERVER['COMPUTERNAME']) == 'csdbbcccw001') {$this->_s_server = 'csdbbmsql001';}
//					else {$this->_s_server = 'csdbbcdev001';}
					else {$this->_s_server = 'CSDBBCDEV001\SQLEXPRESS';}
				}
				else
				{
					$this->_s_username = $username;
					$this->_s_password = $password;
					$this->_s_database = $database;
					$this->_s_server = $server;
				}
				if (strlen($type) == 0) {$this->_s_type = 'mssql';}
				else {$this->_s_type = $type;}
			}
			catch(exception $error)
			{
			}
		}
		
/**
* Destructor method that will disconnect the database and clear any residual objects from memory.
*
* @access public
* @todo Complete exception handling functionality in try-catch blocks.
* @author Mike Bronner <michael.g.bronner@kp.org>
* @version 1
*/
		public function __destruct()
		{
			if ($this->_r_connection) {$this->_r_connection->disconnect();}
			$this->_r_connection	= null;
			$this->_s_database		= null;
			$this->_s_password		= null;
			$this->_s_server		= null;
			$this->_s_type			= null;
			$this->_s_type			= null;
			$this->_s_username		= null;
		}
		
/**
* Method that is called internally when needed to connect to the database. Each method in the class will
* connect, perform its actions against the database, then disconnect again.
*
* @access private
* @todo Complete exception handling functionality in try-catch blocks.
* @author Mike Bronner <michael.g.bronner@kp.org>
* @version 1
*/
		private function connect()
		{
			$dsn = '';
			
			try
			{
				$dsn = $this->_s_type . '://' . $this->_s_username . ':' . $this->_s_password . '@' . $this->_s_server . '/' . $this->_s_database;
				$db =& MDB2::factory($dsn, array('debug' => 2,'result_buffering' => false));
				if (PEAR::isError($db)) {throw new Exception($db->getMessage());}
				else
				{
					$this->_r_connection = $db;
					return true;
				}
			}
			catch(exception $error)
			{
				return false;
			}
		}
		
/**
* Wrapper method that is called internally when needed to disconnect from the database. Each method in the 
* class will connect, perform its actions against the database, then disconnect again.
*
* @access private
* @todo Complete exception handling functionality in try-catch blocks.
* @author Mike Bronner <michael.g.bronner@kp.org>
* @version 1
*/
		private function disconnect()
		{
			$this->_r_connection->disconnect();
		}
		
/**
* Method that is used to run a query.
*
* @access public
* @todo Complete exception handling functionality in try-catch blocks.
* @author Mike Bronner <michael.g.bronner@kp.org>
* @return resource Recordset object.
* @version 1
*/
		public function query($query_string)
		{
			$r_recordset = null;
			
			try
			{
				$this->connect();
				$r_recordset = $this->_r_connection->query($query_string);
				if (PEAR::isError($r_recordset)) {throw new Exception($r_recordset->getDebugInfo());}
				
				return $r_recordset;
			}
			catch (Exception $error)
			{
				return false;
			}
			$this->disconnect();
		}
		
/**
* Method that is used to run DML, like inserts, updates, deletes.
*
* @access public
* @todo Complete exception handling functionality in try-catch blocks.
* @author Mike Bronner <michael.g.bronner@kp.org>
* @return resource Recordset object.
* @version 1
*/
		public function execute($query_string)
		{
			$r_result = null;

			try
			{
				$this->connect();
				$r_result = $this->_r_connection->exec($query_string);
				if (PEAR::isError($r_result)) {throw new Exception($r_result->getDebugInfo());}
				
				return $r_result;
			}
			catch (Exception $error)
			{
				return false;
			}
		}
		
		public function execute_stored_procedure($s_stored_procedure, $a_parameters, $a_datatypes = null)
		{
			$r_resultset = null;
			
			try
			{
				$this->connect();
				$this->_r_connection->loadModule("Function");
				$r_resultset = $this->_r_connection->executeStoredProc($s_stored_procedure, $a_parameters, $a_datatypes, true, true);
				if (MDB2::isError($r_resultset))
				{
echo 'Standard Message: ' . $r_resultset->getMessage() . "<br />";
echo 'Standard Code: ' . $r_resultset->getCode() . "<br />";
echo 'DBMS/User Message: ' . $r_resultset->getUserInfo() . "<br />";
echo 'DBMS/Debug Message: ' . $r_resultset->getDebugInfo() . "<br />";
   				}
			}
			catch (exception $error)
			{
				return false;
			}
			
			return $r_resultset;
		}
	}

Take Care,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top