php4 to php5 conversion: Cannot re-assign $this

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
mikehoy
Forum Newbie
Posts: 2
Joined: Mon Jan 17, 2011 3:50 pm
Location: Minneapolis MN

php4 to php5 conversion: Cannot re-assign $this

Post by mikehoy »

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:

Code: Select all

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
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

Re: php4 to php5 conversion: Cannot re-assign $this

Post by anantha »

Code: Select all

$this = $this->createUnique($new);
assign it to some other variable

eg:

Code: Select all

$test = $this->createUnique($new);
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php4 to php5 conversion: Cannot re-assign $this

Post by Jonah Bron »

$this is a magic variable in PHP since OO was introduced. You cannot assign it. If it makes sense to use that name in your code, you can use $self instead.
mikehoy
Forum Newbie
Posts: 2
Joined: Mon Jan 17, 2011 3:50 pm
Location: Minneapolis MN

Re: php4 to php5 conversion: Cannot re-assign $this

Post by mikehoy »

anantha wrote:

Code: Select all

$this = $this->createUnique($new);
assign it to some other variable

eg:

Code: Select all

$test = $this->createUnique($new);
Thanks anatha this is resolved....
Post Reply