neylitalo Posted July 10, 2006 Share Posted July 10, 2006 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.[code]$organization = $organizationBuilder->getOrganization();$organization2 = $organizationBuilder->getOrganization();[/code]Here, I would assume that I'd have two independent objects, right? Read on...[code]$organization->setName("Test Organization");die($organization2->getName());[/code]And now, I assign $organization a name, and get the name of $organization2. However, it spits out "Test Organization".And if I do this:[code]echo $organization."<br />";echo $organization2."<br />";[/code]I get [quote]Object id #497Object id #497[/quote]So $organization2 and $organization are the same object? I think I'm missing something... can somebody explain? Quote Link to comment https://forums.phpfreaks.com/topic/14209-oop-variables-passed-by-reference-by-default/ Share on other sites More sharing options...
willfitch Posted July 10, 2006 Share Posted July 10, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14209-oop-variables-passed-by-reference-by-default/#findComment-55730 Share on other sites More sharing options...
neylitalo Posted July 10, 2006 Author Share Posted July 10, 2006 Damn. I was afraid of that. Thanks.Is there any way to force it to pass by value? Quote Link to comment https://forums.phpfreaks.com/topic/14209-oop-variables-passed-by-reference-by-default/#findComment-55733 Share on other sites More sharing options...
willfitch Posted July 10, 2006 Share Posted July 10, 2006 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:[code=php:0]<?php$obj1 = new Class();$obj2 = clone $obj1;?>[/code]This should help your issue. Quote Link to comment https://forums.phpfreaks.com/topic/14209-oop-variables-passed-by-reference-by-default/#findComment-55744 Share on other sites More sharing options...
neylitalo Posted July 11, 2006 Author Share Posted July 11, 2006 beautiful, thank you! :) Quote Link to comment https://forums.phpfreaks.com/topic/14209-oop-variables-passed-by-reference-by-default/#findComment-56000 Share on other sites More sharing options...
hvle Posted July 11, 2006 Share Posted July 11, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14209-oop-variables-passed-by-reference-by-default/#findComment-56145 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.