PHP 5 passing by reference, copying, and cloning question

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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

PHP 5 passing by reference, copying, and cloning question

Post by neophyte »

I'm working with PHP5 which passes by reference by default and not by copying.

I have two image resources variables: imgResource and imgNew.

After the image is resized I want to do a little clean up and copy the newly resized image resource from imgNew to imgResource and then destroy imgNew.

However the following code seems to pass by reference.

Thus when I destroy imgNew I also destroy imgResource.

I tried Clone() and (clone) but imgResource isn't an object neither is imgNew and it returns an error.

My question:

What are my options for copying?

Here's the snippet in question.

Code: Select all

if (@imagecopyresampled ( $this->img->getImageNew(), $this->img->imgResource, 0, 0, 0, 0,  $newWidth, $newHeight, $this->img->getImageWidth(), $this->img->getImageHeight() )){
					$this->img->imgResource =  $this->img->imgNew;
					imagedestroy($this->img->imgNew);
				}
I've got the idea of putting the image resource inside an object inside the object. But I'm just wondering If I have other options.

Thanks
User avatar
ibbo
Forum Commoner
Posts: 51
Joined: Tue Sep 19, 2006 6:20 am

Post by ibbo »

When you say pass by reference you are refering to something like

$this->method(&$var); which has been depreciated in php5 and would complian to you about your doing so.

Does it not work by copying your img into local variable and not another class variable like it appears? Whats the point of dropping a class img var and copying it into another var of the same object.

Thats appears to be doing more work and using more resources.

Ibbo
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

ibbo wrote:When you say pass by reference you are refering to something like

$this->method(&$var); which has been depreciated in php5 and would complian to you about your doing so.
PHP5 assignments ('=') are made by reference by default. Where as in PHP4 it is copy by default.

After I do this:

Code: Select all

$this->img->imgResource =  $this->img->imgNew;
Both images properties have the same resource number.
ibbo wrote:Does it not work by copying your img into local variable and not another class variable like it appears? Whats the point of dropping a class img var and copying it into another var of the same object.
Imagine you have a completed imagecopyresampled() resizing an image. You now have two image resources. Now you want to perform additional transformations to the new resized image such as a watermark and then add some type. You have to keep track of which resource is which for final output and which resource to apply the new transformations to. To me it is better to always have the resource to work on in one place hence the copy and destruction. Otherwise you'd end up with some sort of array with: resource 1, resource2, resource3 and so forth.
User avatar
ibbo
Forum Commoner
Posts: 51
Joined: Tue Sep 19, 2006 6:20 am

Post by ibbo »

"Both images properties have the same resource number"

Thats interesting as I would have laid money on them been assigned a seperate address block in memory when created.

"Imagine you have a completed imagecopyresampled() resizing an image. You now have two image resources. Now you want to perform additional transformations to the new resized image such as a watermark and then add some type. You have to keep track of which resource is which for final output and which resource to apply the new transformations to. To me it is better to always have the resource to work on in one place hence the copy and destruction. Otherwise you'd end up with some sort of array with: resource 1, resource2, resource3 and so forth."

Hang on did you not say they both have the same resourceID. Is that the parent object resourceID or both of these images?

Anyway my point been is if you have an image manipulation object as I assumed. Then when creating and manipulating your image resource I would have simply created a local resource (not another image resource) to manipulate the new Image to add watermarks etc to it.

Then if needed it could be copied back to its original parent object variable and used further or destroyed (whichever).

ibbo
Post Reply