Jump to content


Photo

how can I reasign $this reference to point to a different copy of the same class


  • Please log in to reply
6 replies to this topic

#1 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 23 August 2006 - 03:01 PM

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!
..drkstr

PS:
I am very new to PHP, but not new to programming (or programming trickery for that matter)

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 23 August 2006 - 05:20 PM

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.

#3 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 23 August 2006 - 05:28 PM

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.


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...c,104013.0.html

On 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.

#4 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 23 August 2006 - 06:21 PM

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

#5 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 23 August 2006 - 10:34 PM

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.

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
  }
}

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

#6 yyboo

yyboo
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationFlorida

Posted 23 August 2006 - 11:58 PM

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.
-----------------------------
www.mommatown.com

#7 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 24 August 2006 - 01:17 AM

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.

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.

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();

Here's what happens when you run it:
root@lpt:~# php test.php
0 0
1 2

I don't like how klunky this is though. Any one have some better ideas?

Thanks!
...drkstr




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users