Discuss [PHP5] Adding objects to a array in the PHP Development forum on Dev Shed. [PHP5] Adding objects to a array PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
Posts: 193
Time spent in forums: 1 Day 21 h 1 m 53 sec
Reputation Power: 8
[PHP5] Adding objects to a array
Hi
Here is the code I am haveing trouble with:
PHP Code:
public function listAssosiatedTasks(){
global $conn, $changeRequest;
$tasks = array();
$task;
$conn = &ADONewConnection('oci8');
// Attempt connection
if ($c = $conn->Connect(CCC_DB_HOST,CCC_DB_SCHEMA,CCC_DB_PW,CCC_DB_SID)){
$s = $conn->Prepare("SELECT task_id
, status
, owner
, date_created
, action_to
, due_date
FROM task
WHERE crid = :crid");
$conn->InParameter($s,$changeRequest['CRID'],"crid");
// Execute the query
if ($rs = @$conn->Execute($s)){
while($arr = $rs->FetchRow()){
$taskID = $arr['TASK_ID'];
$task = new Task($taskID);
$tasks[] = $task;
}
return $tasks;
}
}
}
what it returns is a array containing copies of the last object to be added. so if i was looping though 3 task objects i would have a array of 3 objects all of which would be the last object repeated. I have added a var dump to the loop and each time a object is added it overwrites (it seems) the other objects in the array.
Last edited by alex905 : December 21st, 2010 at 06:58 AM.
Posts: 9,811
Time spent in forums: 2 Months 3 Weeks 19 h 13 m 52 sec
Reputation Power: 6112
Debugging this on your own will be trivial. Never assume a variable is what you think it is. Echo $taskID inside the loop, see if you're getting 3 different ones.
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Posts: 193
Time spent in forums: 1 Day 21 h 1 m 53 sec
Reputation Power: 8
Yeah I did that I check all the variables at diffrent stanges in this function and the array(in each repition of the loop and outside of it). The task id is unique every time.
Posts: 1,939
Time spent in forums: 1 Month 4 Days 6 h 6 m 13 sec
Reputation Power: 897
edit somehow I edited panda's post instead of replying to it, he originally said something insightful which included:
Quote:
for all we know $val->getInfo() outputs the most recently created ID or something.
Sorry Panda.
__________________
Verify and sanitize ALL USER DATA.
And, to steal a quote from jeremy, "Explain your problem instead of asking how to do what you decided was the solution." Chances are someone on the forums will know a better or more efficient way to do what you're trying to accomplish.
The problem part is the "global $task; ". When you are creating a task, you are doing it like so,
Code:
$task = new Task($taskID);
And in your getInfo function, you are declaring $task as global and returning it. So, the most recently created Task object will be returned and that is reflected in the output.
Some other things:
- It is not a great idea to connect to the database in every single function that has to query the database. Just connect once and reuse that connection.
- The purpose of OOP is to get rid of this "global" business.
Posts: 193
Time spent in forums: 1 Day 21 h 1 m 53 sec
Reputation Power: 8
Not sure I got this right. The global task is in the task class and so is the function that I posted at the top of my last post. In fact its the constructor (yep I just learnt its the wrong way to declare it) I include the global in the function so that when I retrieve the details of task it can be placed in the global. The other function I have (where I create new instances of task) is not in the same file as the task class.
Have I missed something on how globals work? I am new to php.
Can't get to the code again till jan 5th or so.
But I will look over what I posed and see if i can figure out what you meant.