1

This is the fourth time I've tried writing this question, so please bear with me.

I have a PHP object that comes from a DB Query which pulls back the following data:

[1] => stdClass Object
    (
        [eventId] => 11
        [eventName] => Second Event
        [...]
        [eventChildren] => Array
            (
                [0] => stdClass Object
                    (
                        [childId] => 8
                        [childName] => Jane Doe
                        [...]
                        [gifts] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [giftId] => 73
                                        [giftName] => My two front teeth
                                        [childId] => 8
                                        [userId] => 1
                                        [eventId] => 11
                                    )
                                [1] => stdClass Object
                                    (
                                        [giftId] => 74
                                        [giftName] => Wasps
                                        [childId] => 8
                                        [userId] => 1
                                        [eventId] => 11
                                    )

                            )

                    )

            )

    )
)

I'm then running a massive series of foreach loops in order to compare the userId from the gifts array against the userId stored in the session cookie.

From these loops I'm creating an array of children and gifts that the user has selected.

The problem is this overwrites my main object rather than creating a new one.

The Loops:

$user = $this->session->userdata('user');
$tempEvents = $events;
$userSelection = array();
$flag = FALSE;

foreach ( $tempEvents as $i => $event )
{
    if ( $i == 0 )
    {
        foreach ( $event->eventChildren as $child ) 
        {
            $userGift = array();

            foreach ( $child->gifts as $gift )
            {
                if ( $gift->userId == $user['userId'] )
                {
                    array_push($userGift, $gift);
                    $flag = TRUE;
                }
            }

            $tempChild = $child;
            $tempChild->gifts = $userGift;

            if ( $flag )
            {
                array_push($userSelection, $tempChild);
                $flag = FALSE;
            }
        }
    }
}

If I print_r($events); it displays the edited list rather than it's full list of events. Is there a way to create a duplicate object and edit that rather than editing the original object?

1
  • Sounds like you're looking for clone php.net/clone Commented Dec 17, 2011 at 5:00

2 Answers 2

3

The reason for the "overwriting" is $tempChild = $child;.

That will not deep copy the contents of $child but make both $tempChild and $child point towards the same data structure, which obviously isn't desirable in this case.

You should use clone as in the below example.

$tempChild = clone $child;

Sign up to request clarification or add additional context in comments.

4 Comments

When I use clone I get a server 500 error locally. I'm using the newest version of CodeIgniter and running MAMP Pro with PHP 5.3.5
@Seth can you check the error_log for details regarding the internal server error? It should be available at /var/log/apache2/error_log, or similar.
[17-Dec-2011 00:23:32] PHP Fatal error: __clone method called on non-object in /path/to/my/file/site.php on line 24 ($tempEvents = clone $events;)
$events is an array and therefore it's impossible to clone it, revert that line.. I never recommended you to put a clone there, you should be cloneing $child.
0

Try

$tempEvents = clone $events;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.