Jump to content


Photo

OOP: variables passed by reference by default?


  • Please log in to reply
5 replies to this topic

#1 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 10 July 2006 - 07:10 PM

I was wondering if someone could help me out with this - this code and the results lead me to believe that variables are being passed by reference, but my instincts tell me that they shouldn't be.

$organization = $organizationBuilder->getOrganization();
$organization2 = $organizationBuilder->getOrganization();

Here, I would assume that I'd have two independent objects, right? Read on...

$organization->setName("Test Organization");

die($organization2->getName());

And now, I assign $organization a name, and get the name of $organization2. However, it spits out "Test Organization".

And if I do this:
echo $organization."<br />";
echo $organization2."<br />";

I get

Object id #497
Object id #497


So $organization2 and $organization are the same object? I think I'm missing something... can somebody explain?
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#2 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 10 July 2006 - 07:13 PM

In PHP4, they aren't passed by reference unless you append the ampersand (&) to that particular class (i.e. $var = &new Class()).  

In PHP5, the default is to pass by reference.  Also, it depends on the returned value of the method, and whether or not you have cloned that object
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#3 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 10 July 2006 - 07:16 PM

Damn. I was afraid of that. Thanks.

Is there any way to force it to pass by value?
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#4 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 10 July 2006 - 07:27 PM

Hey neylitalo,

As far as I know, you can't stop passing by reference in PHP5.  However, if you are trying to make copies of an object, simply use the "clone" keyword.

Example:

<?php
$obj1 = new Class();
$obj2 = clone $obj1;
?>

This should help your issue.
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#5 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 11 July 2006 - 04:06 AM

beautiful, thank you! :)
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#6 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 11 July 2006 - 12:51 PM

I really think your problem lies inside this function:
$organizationBuilder->getOrganization();

if inside that function, you creates a new instance and return, then you should not have problem.

Life's too short for arguing.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users