Trying to clone a stdClass
Asked Answered
V

3

8

I'm trying to clone a stdClass object that have an attribut which is a DateTime. But it fails. It looks like the clone is not working. Should I wrote my own __clone() method? What is wrong here?

The code:

$object = new stdClass;
$object->date = new DateTime();
var_dump($object->date);

$cloned = clone($object);
$object->date->modify('+1 day');
var_dump($cloned->date);

The output:

object DateTime (
    ->date = string (19) '2013-04-11 11:54:00'
    ->timezone_type = int 3
    ->timezone = string (13) 'Europe/Berlin'

object DateTime (
    ->date = string (19) '2013-04-12 11:54:00'
    ->timezone_type = int 3
    ->timezone = string (13) 'Europe/Berlin'
Voroshilovgrad answered 11/4, 2013 at 9:58 Comment(1)
Why not? It's the day after (11th vs. 12th)Mosher
S
4

When cloning an object, all the object properties are simply copied over to a new instance of the object. In effect this:

$cloned = new stdClass;
$cloned->date = $object->date;

As you probably know, assigning an object to another variable does not duplicate the object; there's still just one object, now with two references to it.

To deep-clone an object you need to implement a custom class with the __clone method and manually clone any child objects of it.

Scab answered 11/4, 2013 at 10:3 Comment(1)
There is a dirty hack to deep clone a stdClass: $cloned = unserialize(serialize($object));Voroshilovgrad
M
12

How to "clone" a php POSCO (Plain Old StdClass Object) via cast chaining:

$cloneObj = (object) (array) $myPOSCO;
var_dump($cloneObj == $myPOSCO); // true
var_dump($cloneObj === $myPOSCO); // false
Midsummer answered 7/5, 2015 at 22:5 Comment(1)
Doesn't work for me, still references the original obj?Sliwa
S
4

When cloning an object, all the object properties are simply copied over to a new instance of the object. In effect this:

$cloned = new stdClass;
$cloned->date = $object->date;

As you probably know, assigning an object to another variable does not duplicate the object; there's still just one object, now with two references to it.

To deep-clone an object you need to implement a custom class with the __clone method and manually clone any child objects of it.

Scab answered 11/4, 2013 at 10:3 Comment(1)
There is a dirty hack to deep clone a stdClass: $cloned = unserialize(serialize($object));Voroshilovgrad
F
-1

There is another simplier option:

$object = new stdClass;
$object->date = new DateTime();
var_dump($object->date);

$cloned = clone $object;
$object->date->modify('+1 day');

var_dump($cloned->date);
var_dump($object->date);

The output must be:

    object DateTime (
        ->date = string (19) '2013-04-11 11:54:00'
        ->timezone_type = int 3
        ->timezone = string (13) 'Europe/Berlin'

    object DateTime (
        ->date = string (19) '2013-04-11 11:54:00'
        ->timezone_type = int 3
        ->timezone = string (13) 'Europe/Berlin'

    object DateTime (
        ->date = string (19) '2013-04-12 11:54:00'
        ->timezone_type = int 3
        ->timezone = string (13) 'Europe/Berlin'
Felice answered 27/2, 2019 at 19:16 Comment(1)
See, I double checked this answer and worked for me, so I won't remove this answer because can help somebody.Felice

© 2022 - 2024 — McMap. All rights reserved.