Jump to content

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


drkstr

Recommended Posts

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)
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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.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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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.php
0 0
1 2

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

Thanks!
...drkstr
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.