Jump to content

php4 to php5 conversion: Cannot re-assign $this


mikehoy

Recommended Posts

I am trying to convert a php4 app to php5 and am getting this error (Fatal error: Cannot re-assign $this in ) when I call this class.

 

$this = $this->createUnique($new); is the line that generates the error.

 

I try to rename $this to $_this but it does not help. I think I lose the reference to the object later on in the code.

 

Then I will get an error like (Fatal error: Call to a member function createUnique() on a non-object in)

 

Is there another way to reference the current instance of an object in php5?

 

Any help appreciated...

 

 

 

Code Example:

class imageobject{

var $handle;
var $height=0;
var $width=0;
var $directory;
var $filename;


//constructor
function imageobject($directory,$filename,$width=0,$height=0,$color="FFFFFF")
{
$this->directory = $directory;
$this->filename = $filename;

if ($filename=="" && $width>0 && $height>0){

$new = $this->createImage($width,$height);
$this = $this->createUnique($new);

}elseif (file_exists($directory.$filename)){
$size = GetImageSize($directory.$filename);
if ($size) $this->handle = $this->getHandle($directory.$filename,$size[2]);
$this->width = $size[0];
$this->height = $size[1];

}


function createUnique($imgnew)
{
$this->type = substr($this->output,0,3);

$unique_str = $this->uniqueName();
switch ($this->type){
case "png":
imagepng($imgnew,RES_DIR.$unique_str);
break;
default:
imagejpeg($imgnew,RES_DIR.$unique_str,$this->quality);
break;
}

$this->handle && imagedestroy($this->handle);
$newobject = new imageobject(RES_DIR,$unique_str,$this->type);
return $newobject;
}

)//end class

I changed the variable name to $_this

 

Then I get an error (Fatal error: Call to a member function createUnique() on a non-object in) in this line

 

$new = $this->createImage($width,$height);

 

Maybe learn PHP first? AbraCadaver already told you how to solve the problem:

 

You can't use $this as a variable name, it is reserved:

 

$something_else = $this->createUnique($new);

I changed the variable name to $_this

 

Then I get an error (Fatal error: Call to a member function createUnique() on a non-object in) in this line

 

$new = $this->createImage($width,$height);

 

Maybe learn PHP first? AbraCadaver already told you how to solve the problem:

 

You can't use $this as a variable name, it is reserved:

 

$something_else = $this->createUnique($new);

 

 

Thanks AbraCadaver that did solve this issue...

  • 1 year later...

@Yasoymama, you need to start your own threads for your coding problems so that you can find and track your own threads. I have split your post in this thread into its own thread in this same forum section.

 

Locking this thread...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.