Strange class OOP behavior

Strange class OOP behavior

am 27.08.2007 16:25:36 von Marcel Molenaar

Anyone ever experienced this problem.

When i pass a mysql result to the constructor of a class and use that
resultset inside that class the original resultset outside the class gets
affected too. That is not right i think because my result inside my class is
private. Is this a bug? Can someone look at my code below please:


require_once('includes/connect.php');

class test {

private $_result;
private $_record;

public function __construct($result) {

$this->_result = $result;

}

public function show() {

while($this->_record = mysql_fetch_array($this->_result)) {

echo $this->_record['name'].'
';

}

}

}


# usage
# sql statement dat artikels ophaald uit cartal database
$SQL = "SELECT * FROM test_table";

# uitvoeren van dit statement
$RESULT = mysql_query($SQL,$conn);

$t = new test($RESULT);

$t->show();

// mysql_data_seek($RESULT,0);

// this prints out nothing unless i call mysql_data_seek($RESULT,0) before
this whileloop so that means that the pointer of the recordset had moved to
the end because of the while loop in the class itself...that is strange!
while($RECORD = mysql_fetch_array($RESULT)) {

echo $RECORD['name'].'
';

}

?>

Re: Strange class OOP behavior

am 27.08.2007 16:46:13 von zeldorblat

On Aug 27, 10:25 am, "Marcel Molenaar" wrote:
> Anyone ever experienced this problem.
>
> When i pass a mysql result to the constructor of a class and use that
> resultset inside that class the original resultset outside the class gets
> affected too. That is not right i think because my result inside my class is
> private. Is this a bug? Can someone look at my code below please:
>
> >
> require_once('includes/connect.php');
>
> class test {
>
> private $_result;
> private $_record;
>
> public function __construct($result) {
>
> $this->_result = $result;
>
> }
>
> public function show() {
>
> while($this->_record = mysql_fetch_array($this->_result)) {
>
> echo $this->_record['name'].'
';
>
> }
>
> }
>
> }
>
> # usage
> # sql statement dat artikels ophaald uit cartal database
> $SQL = "SELECT * FROM test_table";
>
> # uitvoeren van dit statement
> $RESULT = mysql_query($SQL,$conn);
>
> $t = new test($RESULT);
>
> $t->show();
>
> // mysql_data_seek($RESULT,0);
>
> // this prints out nothing unless i call mysql_data_seek($RESULT,0) before
> this whileloop so that means that the pointer of the recordset had moved to
> the end because of the while loop in the class itself...that is strange!
> while($RECORD = mysql_fetch_array($RESULT)) {
>
> echo $RECORD['name'].'
';
>
> }
>
> ?>

The result is a resource, so there's only one of them to pass around.
It doesn't make a copy of it when you pass it into the class.

Re: Strange class OOP behavior

am 27.08.2007 16:46:13 von zeldorblat

On Aug 27, 10:25 am, "Marcel Molenaar" wrote:
> Anyone ever experienced this problem.
>
> When i pass a mysql result to the constructor of a class and use that
> resultset inside that class the original resultset outside the class gets
> affected too. That is not right i think because my result inside my class is
> private. Is this a bug? Can someone look at my code below please:
>
> >
> require_once('includes/connect.php');
>
> class test {
>
> private $_result;
> private $_record;
>
> public function __construct($result) {
>
> $this->_result = $result;
>
> }
>
> public function show() {
>
> while($this->_record = mysql_fetch_array($this->_result)) {
>
> echo $this->_record['name'].'
';
>
> }
>
> }
>
> }
>
> # usage
> # sql statement dat artikels ophaald uit cartal database
> $SQL = "SELECT * FROM test_table";
>
> # uitvoeren van dit statement
> $RESULT = mysql_query($SQL,$conn);
>
> $t = new test($RESULT);
>
> $t->show();
>
> // mysql_data_seek($RESULT,0);
>
> // this prints out nothing unless i call mysql_data_seek($RESULT,0) before
> this whileloop so that means that the pointer of the recordset had moved to
> the end because of the while loop in the class itself...that is strange!
> while($RECORD = mysql_fetch_array($RESULT)) {
>
> echo $RECORD['name'].'
';
>
> }
>
> ?>

The result is a resource, so there's only one of them to pass around.
It doesn't make a copy of it when you pass it into the class.

Re: Strange class OOP behavior

am 27.08.2007 16:50:23 von gosha bine

On 27.08.2007 16:25 Marcel Molenaar wrote:
> Anyone ever experienced this problem.
>
> When i pass a mysql result to the constructor of a class and use that
> resultset inside that class the original resultset outside the class gets
> affected too. That is not right i think because my result inside my class is
> private. Is this a bug?

No, this is the expected behaviour. Resources, like db connections or
result handles are always assigned and passed as pointers, that is,
changing one variable will affect another one if both point to the same
object.


--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi

Re: Strange class OOP behavior

am 27.08.2007 16:58:59 von Marcel Molenaar

"ZeldorBlat" wrote in message
news:1188225973.664601.24310@y42g2000hsy.googlegroups.com...
> On Aug 27, 10:25 am, "Marcel Molenaar" wrote:
>> Anyone ever experienced this problem.
>>
>> When i pass a mysql result to the constructor of a class and use that
>> resultset inside that class the original resultset outside the class gets
>> affected too. That is not right i think because my result inside my class
>> is
>> private. Is this a bug? Can someone look at my code below please:
>>
>> >>
>> require_once('includes/connect.php');
>>
>> class test {
>>
>> private $_result;
>> private $_record;
>>
>> public function __construct($result) {
>>
>> $this->_result = $result;
>>
>> }
>>
>> public function show() {
>>
>> while($this->_record = mysql_fetch_array($this->_result)) {
>>
>> echo $this->_record['name'].'
';
>>
>> }
>>
>> }
>>
>> }
>>
>> # usage
>> # sql statement dat artikels ophaald uit cartal database
>> $SQL = "SELECT * FROM test_table";
>>
>> # uitvoeren van dit statement
>> $RESULT = mysql_query($SQL,$conn);
>>
>> $t = new test($RESULT);
>>
>> $t->show();
>>
>> // mysql_data_seek($RESULT,0);
>>
>> // this prints out nothing unless i call mysql_data_seek($RESULT,0)
>> before
>> this whileloop so that means that the pointer of the recordset had moved
>> to
>> the end because of the while loop in the class itself...that is strange!
>> while($RECORD = mysql_fetch_array($RESULT)) {
>>
>> echo $RECORD['name'].'
';
>>
>> }
>>
>> ?>
>
> The result is a resource, so there's only one of them to pass around.
> It doesn't make a copy of it when you pass it into the class.
>

Thanks!

But is it possible to copy it into a new temporary resource so the original
result will stay untouched?

Marcel

Re: Strange class OOP behavior

am 27.08.2007 16:58:59 von Marcel Molenaar

"ZeldorBlat" wrote in message
news:1188225973.664601.24310@y42g2000hsy.googlegroups.com...
> On Aug 27, 10:25 am, "Marcel Molenaar" wrote:
>> Anyone ever experienced this problem.
>>
>> When i pass a mysql result to the constructor of a class and use that
>> resultset inside that class the original resultset outside the class gets
>> affected too. That is not right i think because my result inside my class
>> is
>> private. Is this a bug? Can someone look at my code below please:
>>
>> >>
>> require_once('includes/connect.php');
>>
>> class test {
>>
>> private $_result;
>> private $_record;
>>
>> public function __construct($result) {
>>
>> $this->_result = $result;
>>
>> }
>>
>> public function show() {
>>
>> while($this->_record = mysql_fetch_array($this->_result)) {
>>
>> echo $this->_record['name'].'
';
>>
>> }
>>
>> }
>>
>> }
>>
>> # usage
>> # sql statement dat artikels ophaald uit cartal database
>> $SQL = "SELECT * FROM test_table";
>>
>> # uitvoeren van dit statement
>> $RESULT = mysql_query($SQL,$conn);
>>
>> $t = new test($RESULT);
>>
>> $t->show();
>>
>> // mysql_data_seek($RESULT,0);
>>
>> // this prints out nothing unless i call mysql_data_seek($RESULT,0)
>> before
>> this whileloop so that means that the pointer of the recordset had moved
>> to
>> the end because of the while loop in the class itself...that is strange!
>> while($RECORD = mysql_fetch_array($RESULT)) {
>>
>> echo $RECORD['name'].'
';
>>
>> }
>>
>> ?>
>
> The result is a resource, so there's only one of them to pass around.
> It doesn't make a copy of it when you pass it into the class.
>

Thanks!

But is it possible to copy it into a new temporary resource so the original
result will stay untouched?

Marcel

Re: Strange class OOP behavior

am 27.08.2007 17:14:02 von Jerry Stuckle

Marcel Molenaar wrote:
> "ZeldorBlat" wrote in message
> news:1188225973.664601.24310@y42g2000hsy.googlegroups.com...
>> On Aug 27, 10:25 am, "Marcel Molenaar" wrote:
>>> Anyone ever experienced this problem.
>>>
>>> When i pass a mysql result to the constructor of a class and use that
>>> resultset inside that class the original resultset outside the class gets
>>> affected too. That is not right i think because my result inside my class
>>> is
>>> private. Is this a bug? Can someone look at my code below please:
>>>
>>> >>>
>>> require_once('includes/connect.php');
>>>
>>> class test {
>>>
>>> private $_result;
>>> private $_record;
>>>
>>> public function __construct($result) {
>>>
>>> $this->_result = $result;
>>>
>>> }
>>>
>>> public function show() {
>>>
>>> while($this->_record = mysql_fetch_array($this->_result)) {
>>>
>>> echo $this->_record['name'].'
';
>>>
>>> }
>>>
>>> }
>>>
>>> }
>>>
>>> # usage
>>> # sql statement dat artikels ophaald uit cartal database
>>> $SQL = "SELECT * FROM test_table";
>>>
>>> # uitvoeren van dit statement
>>> $RESULT = mysql_query($SQL,$conn);
>>>
>>> $t = new test($RESULT);
>>>
>>> $t->show();
>>>
>>> // mysql_data_seek($RESULT,0);
>>>
>>> // this prints out nothing unless i call mysql_data_seek($RESULT,0)
>>> before
>>> this whileloop so that means that the pointer of the recordset had moved
>>> to
>>> the end because of the while loop in the class itself...that is strange!
>>> while($RECORD = mysql_fetch_array($RESULT)) {
>>>
>>> echo $RECORD['name'].'
';
>>>
>>> }
>>>
>>> ?>
>> The result is a resource, so there's only one of them to pass around.
>> It doesn't make a copy of it when you pass it into the class.
>>
>
> Thanks!
>
> But is it possible to copy it into a new temporary resource so the original
> result will stay untouched?
>
> Marcel
>
>

You can make a copy, but since this isn't your actual data, but a
reference to data residing in MySQL, it won't do any good.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Strange class OOP behavior

am 27.08.2007 17:14:02 von Jerry Stuckle

Marcel Molenaar wrote:
> "ZeldorBlat" wrote in message
> news:1188225973.664601.24310@y42g2000hsy.googlegroups.com...
>> On Aug 27, 10:25 am, "Marcel Molenaar" wrote:
>>> Anyone ever experienced this problem.
>>>
>>> When i pass a mysql result to the constructor of a class and use that
>>> resultset inside that class the original resultset outside the class gets
>>> affected too. That is not right i think because my result inside my class
>>> is
>>> private. Is this a bug? Can someone look at my code below please:
>>>
>>> >>>
>>> require_once('includes/connect.php');
>>>
>>> class test {
>>>
>>> private $_result;
>>> private $_record;
>>>
>>> public function __construct($result) {
>>>
>>> $this->_result = $result;
>>>
>>> }
>>>
>>> public function show() {
>>>
>>> while($this->_record = mysql_fetch_array($this->_result)) {
>>>
>>> echo $this->_record['name'].'
';
>>>
>>> }
>>>
>>> }
>>>
>>> }
>>>
>>> # usage
>>> # sql statement dat artikels ophaald uit cartal database
>>> $SQL = "SELECT * FROM test_table";
>>>
>>> # uitvoeren van dit statement
>>> $RESULT = mysql_query($SQL,$conn);
>>>
>>> $t = new test($RESULT);
>>>
>>> $t->show();
>>>
>>> // mysql_data_seek($RESULT,0);
>>>
>>> // this prints out nothing unless i call mysql_data_seek($RESULT,0)
>>> before
>>> this whileloop so that means that the pointer of the recordset had moved
>>> to
>>> the end because of the while loop in the class itself...that is strange!
>>> while($RECORD = mysql_fetch_array($RESULT)) {
>>>
>>> echo $RECORD['name'].'
';
>>>
>>> }
>>>
>>> ?>
>> The result is a resource, so there's only one of them to pass around.
>> It doesn't make a copy of it when you pass it into the class.
>>
>
> Thanks!
>
> But is it possible to copy it into a new temporary resource so the original
> result will stay untouched?
>
> Marcel
>
>

You can make a copy, but since this isn't your actual data, but a
reference to data residing in MySQL, it won't do any good.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Strange class OOP behavior

am 28.08.2007 09:14:04 von Marcel Molenaar

"Jerry Stuckle" wrote in message
news:VMGdnYHK5OIFd0_bnZ2dnUVZ_uXinZ2d@comcast.com...
> Marcel Molenaar wrote:
>> "ZeldorBlat" wrote in message
>> news:1188225973.664601.24310@y42g2000hsy.googlegroups.com...
>>> On Aug 27, 10:25 am, "Marcel Molenaar" wrote:
>>>> Anyone ever experienced this problem.
>>>>
>>>> When i pass a mysql result to the constructor of a class and use that
>>>> resultset inside that class the original resultset outside the class
>>>> gets
>>>> affected too. That is not right i think because my result inside my
>>>> class is
>>>> private. Is this a bug? Can someone look at my code below please:
>>>>
>>>> >>>>
>>>> require_once('includes/connect.php');
>>>>
>>>> class test {
>>>>
>>>> private $_result;
>>>> private $_record;
>>>>
>>>> public function __construct($result) {
>>>>
>>>> $this->_result = $result;
>>>>
>>>> }
>>>>
>>>> public function show() {
>>>>
>>>> while($this->_record = mysql_fetch_array($this->_result)) {
>>>>
>>>> echo $this->_record['name'].'
';
>>>>
>>>> }
>>>>
>>>> }
>>>>
>>>> }
>>>>
>>>> # usage
>>>> # sql statement dat artikels ophaald uit cartal database
>>>> $SQL = "SELECT * FROM test_table";
>>>>
>>>> # uitvoeren van dit statement
>>>> $RESULT = mysql_query($SQL,$conn);
>>>>
>>>> $t = new test($RESULT);
>>>>
>>>> $t->show();
>>>>
>>>> // mysql_data_seek($RESULT,0);
>>>>
>>>> // this prints out nothing unless i call mysql_data_seek($RESULT,0)
>>>> before
>>>> this whileloop so that means that the pointer of the recordset had
>>>> moved to
>>>> the end because of the while loop in the class itself...that is
>>>> strange!
>>>> while($RECORD = mysql_fetch_array($RESULT)) {
>>>>
>>>> echo $RECORD['name'].'
';
>>>>
>>>> }
>>>>
>>>> ?>
>>> The result is a resource, so there's only one of them to pass around.
>>> It doesn't make a copy of it when you pass it into the class.
>>>
>>
>> Thanks!
>>
>> But is it possible to copy it into a new temporary resource so the
>> original result will stay untouched?
>>
>> Marcel
>
> You can make a copy, but since this isn't your actual data, but a
> reference to data residing in MySQL, it won't do any good.
>
> --

Thanks for your help!

Re: Strange class OOP behavior

am 28.08.2007 09:14:04 von Marcel Molenaar

"Jerry Stuckle" wrote in message
news:VMGdnYHK5OIFd0_bnZ2dnUVZ_uXinZ2d@comcast.com...
> Marcel Molenaar wrote:
>> "ZeldorBlat" wrote in message
>> news:1188225973.664601.24310@y42g2000hsy.googlegroups.com...
>>> On Aug 27, 10:25 am, "Marcel Molenaar" wrote:
>>>> Anyone ever experienced this problem.
>>>>
>>>> When i pass a mysql result to the constructor of a class and use that
>>>> resultset inside that class the original resultset outside the class
>>>> gets
>>>> affected too. That is not right i think because my result inside my
>>>> class is
>>>> private. Is this a bug? Can someone look at my code below please:
>>>>
>>>> >>>>
>>>> require_once('includes/connect.php');
>>>>
>>>> class test {
>>>>
>>>> private $_result;
>>>> private $_record;
>>>>
>>>> public function __construct($result) {
>>>>
>>>> $this->_result = $result;
>>>>
>>>> }
>>>>
>>>> public function show() {
>>>>
>>>> while($this->_record = mysql_fetch_array($this->_result)) {
>>>>
>>>> echo $this->_record['name'].'
';
>>>>
>>>> }
>>>>
>>>> }
>>>>
>>>> }
>>>>
>>>> # usage
>>>> # sql statement dat artikels ophaald uit cartal database
>>>> $SQL = "SELECT * FROM test_table";
>>>>
>>>> # uitvoeren van dit statement
>>>> $RESULT = mysql_query($SQL,$conn);
>>>>
>>>> $t = new test($RESULT);
>>>>
>>>> $t->show();
>>>>
>>>> // mysql_data_seek($RESULT,0);
>>>>
>>>> // this prints out nothing unless i call mysql_data_seek($RESULT,0)
>>>> before
>>>> this whileloop so that means that the pointer of the recordset had
>>>> moved to
>>>> the end because of the while loop in the class itself...that is
>>>> strange!
>>>> while($RECORD = mysql_fetch_array($RESULT)) {
>>>>
>>>> echo $RECORD['name'].'
';
>>>>
>>>> }
>>>>
>>>> ?>
>>> The result is a resource, so there's only one of them to pass around.
>>> It doesn't make a copy of it when you pass it into the class.
>>>
>>
>> Thanks!
>>
>> But is it possible to copy it into a new temporary resource so the
>> original result will stay untouched?
>>
>> Marcel
>
> You can make a copy, but since this isn't your actual data, but a
> reference to data residing in MySQL, it won't do any good.
>
> --

Thanks for your help!