Object assignment

thread: 11 messages  |  last: about 5 years ago  |  started: wednesday, january 31, 2007, 5:19 pm pst


#1  |  David B (Adelaide, SA) Australia
Wednesday, January 31, 2007, 5:19 PM PST

Hi all,

I have searched the forums for something about this, but I don't know quite what to look for. I would like to know whether something like this is safe to do:

$objMyObject = MyObject::LoadById(3);

$objCopy = $objMyObject; // is this safe?
$objCopy2 = &$objMyObject; // is this safe?

There is a PHP clone method as well, and something about internal pointers messing things up with this kind of assignment. Can anyone tell me if the above is safe, and what the new objects will contain?

Thanks,
David

#2  |  Mike Ho (San Diego, CA) United States of America Qcodo Administrator
Thursday, February 1, 2007, 9:18 AM PST

David, you might want to read through <http://www.php.net/manual/en/language.oop5.php> for a better undrestanding of objects in PHP5 in general.

But specifically for your question - please note that object variables are always pointers to objects.  A variable never is the object itself - only a pointer to one.

Therefore, $objCopy = $objMyObject does not make a copy of MyObject.  It only makes a copy of the pointer to the object pointed to by $objMyObject in $objCopy.  Or in other words, $objCopy and $objMyObject are now pointing to the same exact object.

$objCopy2 = &$objMyObject is just not a good idea.  And I actually have no idea what it will do.

If you want to make a copy of an object, you should use clone(): <http://www.php.net/manual/en/language.oop5.cloning.php>.  All the warnings about cloning objects with pointers to resources doesn't really apply here.  Data class objects in your qcodo code-generated ORM are freestanding.

HOWEVER -- note that a cloned object will still have the exact same Primary Key ID as the object that you copied.  So it will still refer to the exact same row in the database.

#3  |  David B (Adelaide, SA) Australia
Thursday, February 1, 2007, 6:18 PM PST

Thanks a lot for that Mike, I will do more reading.

David

#4  |  enzo - Eduardo Garcia (San Jos) Colombia
Tuesday, December 11, 2007, 1:17 PM PST

Hi folks

But if I made the following operation

 $objOne = Person::Load(1);
 $objTwo = new Person();
 $objTwo = $objOne;
 $objTwo->Id = 2;
 $objTwo->Save();

My idea is only change the key info. if exist a best way to do this please drop a post.

Best regards.

#5  |  David B (Adelaide, SA) Australia
Tuesday, December 11, 2007, 2:47 PM PST

enzolutions,

If you're using mysql or similar, you probably have the Id field set as an 'auto increment' field type. In this case, I believe QCodo does not generate a __set() method for ->Id, so you cannot change it directly.

You are, however, close I think - I use a similar method to save a copy of the same object:

[code]
$objTwo = Person::LoadById(1); // this is $objOne's contents
$objTwo->Save(true); // save forcing 'insert', which creates a new record
[/code]

At this point we have $objTwo as a copy of $objOne, and with a new Id (which QCodo and your database will decide on).

Hope that helps...
David

#6  |  alex94040 (Seattle, WA) United States of America Qcodo Core Contributor
Wednesday, December 26, 2007, 10:12 AM PST

Funny, I just came across the need to do use the clone() PHP function for the first time in a long, long time. And just like David B says, I've done a ->Save(true) to do a “force insert”.

I'm wondering if a tiny addition of

public function __clone() {
    $this->Save(true); // re-save, forcing insert
}

into the Codegen templates would save someone 10 minutes of a headache. Mike?

#7  |  Mike Ho (San Diego, CA) United States of America Qcodo Administrator
Friday, December 28, 2007, 8:28 PM PST

It might... =)  but it also might cause some too.

depending on why / how people are using cloned objects... it might be counter-intuitive to automatically do a save for a new record to the database when the dev only wanted to do a in-memory clone of the object.

I would think ,by definition, clone should not assume a change to the database unless explicitly stated to do so.

#8  |  alex94040 (Seattle, WA) United States of America Qcodo Core Contributor
Friday, December 28, 2007, 9:13 PM PST

You're totally right. I take my suggestion back :)

#9  |  tronics (VIE, AUT) Austria
Sunday, February 10, 2008, 6:50 PM PST

Ok, but this is a naming issue.
The functionality would make a lot of sense in the core!

Please Mike, add a function cloneToDatabase ;) into the core to save me more headaches :)

#10  |  VexedPanda (Calgary, AB) Canada
Tuesday, February 12, 2008, 7:53 AM PST

There IS this functionality in the core. It's called Save(true), instead of __clone(), but it's there... If the function name is important to you, you can write your own wrapper function into the templates.



Copyright © 2005 - 2013, Quasidea Development, LLC
This open-source framework for PHP is released under the terms of The MIT License.