OO: error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

OO: error

Post by raghavan20 »

Code: Select all

<?php
//DB.class.php

class DbMysql{
	var $hostName;
	var $userName;
	var $password;
	var $dbName;
	var $dbh;//database connection handle
	
//constructor and connecting to the database not shown	
	function executeQuery($query){
		if (!is_resource($this->dbh)){
			$this->connectDB();//connect to the db if not connected
		}
		$stmt = new DbMysqlStatement($this->dbh, $query);
		return $stmt;//return an object of DbMysqlStatement
	}
}


class DbMysqlStatement{
	var $query;
	var $result;
	var $dbh;
	
	function DbMysqlStatement($dbh, $query, $result = ""){
		echo "dbmysqlstatement  constructor called!!!";
		$this->dbh = $dbh;
		$this->query = $query;
		if (!is_resource($this->result)){
			$this->computeResult();//compute result if not computed before			
		}
	}
	
	function computeResult(){
		echo "computeresult called!!!";
		if (is_resource($this->dbh)){
			$this->result = mysql_query($this->query, $this->dbh);
		}
	}
	
	function fetchRow(){
		echo "dbmysqlstatement  fetchrow called!!!";
		if (is_resource($this->result)){
			return mysql_fetch_row($this->result);//return a row
		}
	}
	
	function fetchAssoc(){
		if (is_resource($this->result)){
			return mysql_fetch_row($this->result);//return an associative array
		}
	}
	
}
?>

<?php
//dbAccess.php

include("DB.class.php");

$dbInstance = new DbMysql();
$dbInstance->connectDb();
$query = "select * from Members_tbl";
$tempArray = array();
$tempArray = $dbInstance->executeQuery($query)->fetchRow();//this doesnot run
//but this runs perfectly and displays all the details in the Members_tbl
//$stmt = $dbInstance->executeQuery($query);
//$tempArray = $stmt->fetchRow();
$iterations = count($tempArray);
//echo $iterations;
for ($i = 0; $i < $iterations; $i++){
	echo "<br />".$tempArray[$i];
}

?>
You could that in the second file I have commented a line

Code: Select all

$tempArray = $dbInstance->executeQuery($query)->fetchRow();

Code: Select all

which doesnt run but the second set of two lines behind that runs. Why is that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

last I checked, php doesn't allow multiple object redirection. Seperate that line into two, and you should be fine.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

$dbInstance->executeQuery($query)->fetchRow();
The first part on execution[$dbInstance->executeQuery($query)] returns an object of the second class.
so I thought [$secondClassObject->fetchRow()] should work.

Is this kind of syntax allowed in PHP 5?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

this worked in 5.0.4

Code: Select all

<?php

class foo
{
	function sayHi()
	{
		echo "Hi";
	}
}

class bar
{
	function &fooMe()
	{
		$this->foo =& new foo();
		return $this->foo;
	}
}

$barMe =& new bar();
$barMe->fooMe()->sayHi();

?>
However, you should be very careful when doing this kind of code. If the first method returns something other than the object expected, you're in a world of hurt. It's better to play it safe and store off the return from the first method to make sure it's an object of type you wanted.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

What are the meanings of the following lines?
I have not seen reference operators over those places. Please clarify.

Code: Select all

$barMe =& new bar();

Code: Select all

function &fooMe() 
    { 
        $this->foo =& new foo(); 
        return $this->foo; 
    }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

raghavan20 wrote:What are the meanings of the following lines?
I have not seen reference operators over those places. Please clarify.

Code: Select all

$barMe =& new bar();
force the result to be a reference.. avoids some issues with php copying the object instead of storing the reference.

Code: Select all

function &fooMe() 
    { 
        $this->foo =& new foo(); 
        return $this->foo; 
    }
function intends to return a reference. May help php prepare how to handle returns from the function.
Post Reply