For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > August 2007 > PHP OOP Bug?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author PHP OOP Bug?
Marcel Molenaar

2007-08-27, 7:21 pm

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:

<?php

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'].'<br />';

}

}

}


# 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_s($RESULT,0);

// this prints out nothing unless i call mysql_data_s($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'].'<br />';

}

?>



Jan Thomä

2007-08-27, 7:21 pm

Hi Marcel,

first off, you should _never_ call unexpected behaviour a bug unless you are
very sure about it being a bug. Especially not in the subject line. See
http://catb.org/~esr/faqs/smart-questions.html#id270918 for an explanation
why.

Marcel Molenaar wrote:

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


Your class members being private does not have any influence about this. You
give in a result set and your show() function is operating on the *very
same* result set that you give in. Not a magic copy or something. The
private modifier only states, that you cannot access the variable from
outside the class, but thats all. Therefore the behaviour you are seeing is
absolutely what I would expect to see here. To put an analogy:

You put a dog (your resultset) into a magic apparatus (your class). Inside
the box, a little gnome paints your dog green (in the show() method). Now
if you take out the dog again - it won't magically turn to it's natural
color again.

Hope that helps ;)

Best regards,
Jan Thomä

--
________________________________________
_________________________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de

Marcel Molenaar

2007-08-28, 4:37 am


"Jan Thomä" <kork@insomnia-hq.de> wrote in message
news:5jgr51F3tniffU1@mid.individual.net...
> Hi Marcel,
>
> first off, you should _never_ call unexpected behaviour a bug unless you
> are
> very sure about it being a bug. Especially not in the subject line. See
> http://catb.org/~esr/faqs/smart-questions.html#id270918 for an explanation
> why.
>
> Marcel Molenaar wrote:
>
>
> Your class members being private does not have any influence about this.
> You
> give in a result set and your show() function is operating on the *very
> same* result set that you give in. Not a magic copy or something. The
> private modifier only states, that you cannot access the variable from
> outside the class, but thats all. Therefore the behaviour you are seeing
> is
> absolutely what I would expect to see here. To put an analogy:
>
> You put a dog (your resultset) into a magic apparatus (your class). Inside
> the box, a little gnome paints your dog green (in the show() method). Now
> if you take out the dog again - it won't magically turn to it's natural
> color again.
>
> Hope that helps ;)
>
> Best regards,
> Jan Thomä
>
> --


Thanks for your help!


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com