Welcome Guest, Not a member yet? Register   Sign In
Pass objects to array by value
#1

[eluser]obiron2[/eluser]
Guys,

I am writing my first fully OOP application; its a fantasy formula 1 league.

I have an object (PlayerTeam) which is a collection of objects (Driver 1, Driver 2, Chassis etc..)

Each object in the collection has a number of properties (name, points etc..)

I have a function which gets a list of all the teams and builds their profiles.

I have a function which gets the race results.

For each team I then loop through the race results and increment the points for each element of the team so that I know how many points Driver 1, Driver 2 and the car have scored.

I also want to build an array as part of the team object which is the individual race results for that team.

Code:
$team->Race->results[$Race->Venue] = $Race->Results.

but when I do this the result always displays the values from the last $Race; i.e. passed by reference

If I do

Code:
$team->Race->results[$Race->Venue] = $Race->Results->Driver1->RacePoints

Then the results are all different; i.e. passed by value.

Am I right in thinking that this is correct. If so, why does PHP treat these differently and how do I pass the object by value to the array.

Thanks in Advance.

Obiron
#2

[eluser]mironcho[/eluser]
You should keep in mind that if you are using PHP5, objects are always passed by reference. So if that's the case, you have to use clone for object copying.
#3

[eluser]obiron2[/eluser]
How do I use clone.

is this a function or do i need to do $team->Race->results[$Race->Venue]= &new;$Race

thanks

Obiron
#4

[eluser]mironcho[/eluser]
You can use it in that manner:
Code:
class MyClass
{
        public $y;

        function print_y()
        {
                echo "{$this->y} \n";
        }
}

$orig_obj = new MyClass();
$orig_obj->y = 4;

$copy_obj = clone $orig_obj;
$copy_obj->y = 5;

$orig_obj->print_y();
$copy_obj->print_y();

// outputs:
4
5

If it was without clone:
Code:
class MyClass
{
        public $y;

        function print_y()
        {
                echo "{$this->y} \n";
        }
}

$orig_obj = new MyClass();
$orig_obj->y = 4;

$copy_obj =  $orig_obj;
$copy_obj->y = 5;

$orig_obj->print_y();
$copy_obj->print_y();

// outputs:
5
5

You can see the difference Smile
if you want to do something more when cloning, you can implement __clone magic method.
More information about object cloning you can find here:
http://bg2.php.net/manual/en/language.oop5.cloning.php
#5

[eluser]obiron2[/eluser]
OK,

Having done some testing, this still doesn't work for me.

I think because of the following:

The object I am cloning is a collection of objects, which when I do a Var_dump show as follows.

object(stdClass)#44 (6) { ["Driver1"]=> object(stdClass)#43 (8) { ["ID"]=> string(2) "11" ["Name"]=> string(12) "R Schumacher" ["Qpos"]=> int(0) ["Rpos"]=> int(0) ["Qpoints"]=> int(0) ["Rpoints"]=> int(0) ["Overtaking"]=> int(0) ["Points"]=> int(0) }.....}

When I clone object#44 it gets a new number lets say #99, but the objects in the new object still reference the same object numbers

object(stdClass)#99 (6) { ["Driver1"]=> object(stdClass)#43 (8) { ["ID"]=> string(2) "11" ["Name"]=> string(12) "R Schumacher" ["Qpos"]=> int(0) ["Rpos"]=> int(0) ["Qpoints"]=> int(0) ["Rpoints"]=> int(0) ["Overtaking"]=> int(0) ["Points"]=> int(0) }.....}

so when I rerun the query and the contents of object #43 changes, the data still changes in both the original and cloned object.

Without writing a function to iterate through the collection and clone every object in it, is there an easier way to achieve what I am trying to do?

Obiron
#6

[eluser]mironcho[/eluser]
You could define __clone() method in your class and clone all objects (Driver 1, Driver 2, Chassis etc) into it. If Driver 1 is a collection of objects - define __clone() in Driver 1, and so on...




Theme © iAndrew 2016 - Forum software by © MyBB