Forum Moderators: coopster

Message Too Old, No Replies

Stacking objects in an array with array push()

         

Hugene

4:19 am on Jun 3, 2009 (gmt 0)

10+ Year Member



Just started using OO PHP (finally).

I have an object that queries the DB and populates an array of result objects as follows:


for ($i=0; $i < $num_results; $i++)
{
$comment = new comment();
$row = mysql_fetch_array($result);
$comment->setContent($row);
array_push($comments_array, $comment);
}

But when I pass this $comments_array to another object it is as if the individual elements of $comments_array are empty.

It's as if array_push($comments_array, $comment); pushed a reference to $comment = new comment(); in the array, such that when the objects go out of scope they are lost.

Any suggestion on how to get array_push($comments_array, $comment); to push objects in the array by copy?

Thanks a lot

Hugene

4:27 am on Jun 3, 2009 (gmt 0)

10+ Year Member



Ok, actually just discovered the problem is not with array_push($comments_array, $comment); It's with $comment->setContent($row); where I basically just assign the result of $row = mysql_fetch_array($result); to an object variable. For some reason it doesn't like that.

Hugene

4:47 am on Jun 3, 2009 (gmt 0)

10+ Year Member



Ok, fixed.

I've forgotten about the loose variable managment of PHP:


class Test
{
private $var;
function changeVar($input)
{
$var = $input //NO GOOD
$this->var = $input //VERY GOOD
}
}