i got this while loop into my listByEvent function inside my foto class to retrieve my db fotos:
while ($fetch_query = mysql_fetch_assoc($get_foto)) {
$this -> idFoto = $fetch_query['idFoto'];
$this -> arquivo = $fetch_query['arquivo'];
$this -> legenda = $fetch_query['legenda'];
$this -> idEvento = $fetch_query['idEvento'];
$this -> curtidas = $fetch_query['curtidas'];
$this -> naocurtidas = $fetch_query['naocurtidas'];
$fotos[] = $this;
}
return $fotos;
Then in my view (show.php), i call the method like this:
$foto = new foto();
$foto -> idEvento = $key -> idEvento;
$fotos = $foto -> listByEvent();
foreach ($fotos as $fotokey) {
//here i proper format the layout
}
But the while loop can`t override the $this properties and it retrieves always the same foto. If i change the fc to call a new obj, like this:
while ($fetch_query = mysql_fetch_assoc($get_foto)) {
$jack = new foto();
$jack -> idFoto = $fetch_query['idFoto'];
$jack -> arquivo = $fetch_query['arquivo'];
$jack -> legenda = $fetch_query['legenda'];
$jack -> idEvento = $fetch_query['idEvento'];
$jack -> curtidas = $fetch_query['curtidas'];
$jack -> naocurtidas = $fetch_query['naocurtidas'];
$fotos[] = $jack;
}
return $fotos;
Than it works. Anyone can explain why i cant override this methods on a while loop? Thank you for your time
When you assign class objects, you don't make a copy, you're just assigning a reference to the object. So in your first loop, all the array elements refer to the same $this
object, which you modify each time through the loop. You need to use new
to create new objects so that the array elements will be distinct.
The this pointer is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.
Hence this pointer is returning the same value.
So you need create a new object to hold the values. That's why its working in second loop.