drkstr Posted August 23, 2006 Share Posted August 23, 2006 Hello, sorry if this is the wrong forum, but it seemed like a "core" php work around to me.I have an object method that needs to be able to grab another object of the same class, then reasign all it's elements to the current ($this) object. I would not like to assign each element indavidually since they will change frequently. Looks like php prevents me from "re-pointing" $this, does anyone have any clever work arounds for this?thanks!..drkstrPS:I am very new to PHP, but not new to programming (or programming trickery for that matter) Quote Link to comment https://forums.phpfreaks.com/topic/18433-how-can-i-reasign-this-reference-to-point-to-a-different-copy-of-the-same-class/ Share on other sites More sharing options...
wildteen88 Posted August 23, 2006 Share Posted August 23, 2006 Are you coding in OOP. If you are then $this is a resevered variable. This variable is used in OOP to get variables or functions from within its own class. You might want to use a different variable like $_this, or $This or $THIS all these variables are completly different to php. Quote Link to comment https://forums.phpfreaks.com/topic/18433-how-can-i-reasign-this-reference-to-point-to-a-different-copy-of-the-same-class/#findComment-79349 Share on other sites More sharing options...
448191 Posted August 23, 2006 Share Posted August 23, 2006 [quote author=wildteen88 link=topic=105353.msg420837#msg420837 date=1156353642]Are you coding in OOP. If you are then $this is a resevered variable. This variable is used in OOP to get variables or functions from within its own class. You might want to use a different variable like $_this or something.[/quote]Yes, he is. If someone could accomplish what he's trying to do, I'd like to know too. That'd fix my problem here: http://www.phpfreaks.com/forums/index.php/topic,104013.0.htmlOn a sidenote: according to the manual, $this is only a reserved keyword in php5. So what you're trying to do might just work in php4. Quote Link to comment https://forums.phpfreaks.com/topic/18433-how-can-i-reasign-this-reference-to-point-to-a-different-copy-of-the-same-class/#findComment-79354 Share on other sites More sharing options...
448191 Posted August 23, 2006 Share Posted August 23, 2006 What we need to do is merge objects. It doesn't look like we can import methods from within a class, so we'd have to use a parent class to imitate a multiple extend (sort of). This might be of interest: http://satria.web.id/object_merge.html Quote Link to comment https://forums.phpfreaks.com/topic/18433-how-can-i-reasign-this-reference-to-point-to-a-different-copy-of-the-same-class/#findComment-79383 Share on other sites More sharing options...
drkstr Posted August 23, 2006 Author Share Posted August 23, 2006 hmm, these "magic functions" are interesting. I will have to remember them for later, but I am not sure if they will help me in this case. Let me try to be a little more detailed about what I'm trying to do by providing a little psuedo code.[code]function &globalFunction( $key ) { ... $obj = new SomeObj(); # free up memory for another object $obj = unserialize( dba_fetch( $key, $database_handle ) ); # read SomeObj from a saved state in a database return $obj; # return the pointer to the newly created object}class SomeObj { ... function updateObj( ) { $pNewObj = & new SomeObj(); # initialize a pointer to a SomeObj $pNewObj = & globalFunction( $strKey ); #$pNewObj now points to the SomeObj created in globalFunction() if ( get_class($pNewObj) == "SomeObj" ) { # make sure it is infact a pointer to the same object $this = $pNewObj # I was hoping that the "current object" would now point to the updated one }}[/code]But unfortunatly, PHP will not let me re-point $this. Perhaps it's a lost cause? I find it hard to belive that php would lack support for object copying/reasigning since this is a pretty standard part of oop. I'm sure I'm just missing something.thanks for the replies!...drkstr Quote Link to comment https://forums.phpfreaks.com/topic/18433-how-can-i-reasign-this-reference-to-point-to-a-different-copy-of-the-same-class/#findComment-79595 Share on other sites More sharing options...
yyboo Posted August 23, 2006 Share Posted August 23, 2006 I had a working site where they upgraded PHP and then most of my pages had an error related to $this so I changed all variables in my code from $this to $thiis and it fixed it all. I learned not to use variables with simple names as the core programming may also use them. Quote Link to comment https://forums.phpfreaks.com/topic/18433-how-can-i-reasign-this-reference-to-point-to-a-different-copy-of-the-same-class/#findComment-79630 Share on other sites More sharing options...
drkstr Posted August 24, 2006 Author Share Posted August 24, 2006 [quote]I had a working site where they upgraded PHP and then most of my pages had an error related to $this so I changed all variables in my code from $this to $thiis and it fixed it all. I learned not to use variables with simple names as the core programming may also use them.[/quote] hehe, a good leson to learn. In my case though, I am am trying to use the "reserved use" of the $this variable.Well on a side note, I think I might have come up with something that will work after getting an idea from the link posted by "448191". It was a lot uglier then what I was hoping for, but at least it will work for what I'm doing, and I will not have to update the code any time new variables are added/removed. note: works with inherated properties as well.[code]function globalFunction() { #for demonstration $newObj = new SomeObj(1, 2); return $newObj; #return our updated object}class SomeObj { private $var1; private $var2; # constructor function SomeObj($_var1 = 0, $_var2 = 0) { #assign default 0 to vars, or items past in constructor $this->var1 = $_var1; $this->var2 = $_var2; } function changeVars() { $newObj = new SomeObj(); #initialize memory for new object $newObj = globalFunction(); #get the updated object from globalFunction() $arMyVars = get_object_vars($newObj); #get an asociative array of each property in SomeObj foreach ($arMyVars as $name => $value) { $this->$name = $value; #assign all the data from $newObj to $this } } function printVars() { print "$this->var1 $this->var2\n"; }}$myObj = new SomeObj();$myObj->printVars();$myObj->changeVars();$myObj->printVars();[/code]Here's what happens when you run it:root@lpt:~# php test.php0 01 2I don't like how klunky this is though. Any one have some better ideas?Thanks!...drkstr Quote Link to comment https://forums.phpfreaks.com/topic/18433-how-can-i-reasign-this-reference-to-point-to-a-different-copy-of-the-same-class/#findComment-79642 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.