Welcome Guest, Not a member yet? Register   Sign In
Strange memory allocation
#1

[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);
        
        foreach($query->result() as $row)
        {
            $message->idForum = $row->idForum;
            $message->idTopic = $row->idTopic;
            $message->idUser = $row->idUser;
            $message->username = $this->getNameById($row->idUser)->username;
            $results[] = $message;
        }
                
        return $results;

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)
        {
            unset($message);
            $message->idForum = $row->idForum;
            $message->idTopic = $row->idTopic;
            $message->idUser = $row->idUser;
            $message->username = $this->getNameById($row->idUser)->username;
            $results[] = $message;
        }
        
        return $results;
    }

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 Confused
#2

[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.
#3

[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)
        {
            $message = new Object();
            $message->idForum = $row->idForum;
            $message->idTopic = $row->idTopic;
            $message->idUser = $row->idUser;
            $message->username = $this->getNameById($row->idUser)->username;
            $results[] = $message;
        }
        
        return $results;
    }

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 {

    public int test()
    {
        Object o = new Object();
        return 1;
    }
}

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.
#4

[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




Theme © iAndrew 2016 - Forum software by © MyBB