Assigning to $this PHP 5

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
raygr
Forum Newbie
Posts: 5
Joined: Mon Jun 30, 2003 3:16 pm

Assigning to $this PHP 5

Post by raygr »

I'm porting some code from PHP 4 to PHP 5. It's not liking this:

$s = _SESSION['x'];
$this = unserialize($s);

The reason I'm doing this is to have a class method called "LoadFromSession". If you have a better way to do this, please tell me.

:?: In general, (in PHP5) I would like to be able to copy over the contents of the current object. Is there a way to assign a new object to the current object (pointed to by $this)? For eg:

$this = $otherobject_of_same_type
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I think $this is a CONSTANT pointer to the current object. (And thus cannot be changed)

Meaby this works
$instance1 = new Class();
$instance2 = new Class();
$instance1 =& $instance2;

http://be2.php.net/references
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$foo =& $bar

creates a reference to $bar.. any change to $bar will change $foo, and vice versa.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

With PHP5 $foo = $bar is ok as it auto assigns by reference rather than creating/copying a new object.
raygr
Forum Newbie
Posts: 5
Joined: Mon Jun 30, 2003 3:16 pm

Post by raygr »

markl999 wrote:With PHP5 $foo = $bar is ok as it auto assigns by reference rather than creating/copying a new object.
Right, you need to use the clone keyword to actually copy an object. But, I want a way to copy an object into the current object.

If this were C++, I want to do this:

*this = object;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

standard internal cloning: copy over each internal variable.
raygr
Forum Newbie
Posts: 5
Joined: Mon Jun 30, 2003 3:16 pm

Post by raygr »

feyd wrote:standard internal cloning: copy over each internal variable.
So, there's no way to do it in one assignment? I have to assign to each member variable?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Right, you need to use the clone keyword to actually copy an object. But, I want a way to copy an object into the current object.

If this were C++, I want to do this:

*this = object;
I'm a bit confused as to why you'd want to copy an object into an existing object, this would in effect overwrite it, so why not just use __clone to copy the object into a variable hence creating a copy of the object. I'm not sure why you need an existing object to copy into ?
I suppose the real question is, when do you ever need a copy of an object?
Post Reply