Strange memory allocation - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Strange memory allocation (/showthread.php?tid=8191) |
Strange memory allocation - El Forum - 05-08-2008 [eluser]Kromack[/eluser] Hello everyone, I have a PHP related question. First, here is my code (it's in a library) Code: $query = $this->object->db->query($sql); I have 3 row returned by the sql query. With this code the $results array contains 3 times the same row. I have checked my query and the foreach, all is ok, but after the $results[] = $message; insctruction, something strange happens... (my $results is parsed by a foreach instruction in a view). I have solved the problem by unsetting the $message : Code: foreach($query->result() as $row) With this code, the 3 different rows are in the $results array. I'm very confused because I can't understand why my first code doesn't work properly... Anybody have an idea ? PS : Sorry for my bad english Strange memory allocation - El Forum - 05-08-2008 [eluser]Seppo[/eluser] You have to reinstantiate the variable... if you are using PHP 5 objects are passed by reference, so you only have one object and you add it to the array three times... Probably that should work under php 4 when objects worked by copy as all the other variables. Strange memory allocation - El Forum - 05-09-2008 [eluser]Kromack[/eluser] Han ! I though it was passed by copy ! So, now I'm wondering what is the best way to make a new Object at each time of the foreach. I have tryed to do this : Code: foreach($query->result() as $row) But I got a blank page issue, I think it's because the class Object doesn't exists. Do you know a way to do something like this (Working in Java) : Code: public class test { Also, I think I must have a message class declared anywhere to do : Code: $message = new Message(); But where do I have to put all business class of my application (for exemple : Client, User, Message etc...) ? Thank's for your help. Strange memory allocation - El Forum - 05-09-2008 [eluser]Seppo[/eluser] PHP has a basic class called "stdClass". If you wanna define your own class, probably a Model would be OK... In the models definition I usually have some other classes related to the model, may be that can help you |