xtopolis Posted October 16, 2008 Share Posted October 16, 2008 I learned some OOP from a book, but it was PHP 4 style afaik. Is this & necessary? <?php $session = &new Session; ?> or does it have the same effect as not having the & there, in PHP 5.2.x ? And also, this means that it creates the same instance if it already exists? or what? Link to comment https://forums.phpfreaks.com/topic/128658-solved-by-ref-not-necessary/ Share on other sites More sharing options...
keeB Posted October 16, 2008 Share Posted October 16, 2008 Nope. It is no longer necessary as all variables are now passed by Reference. The definition in Wikipedia: In call-by-reference evaluation, a function receives an implicit reference to the argument, rather than a copy of its value. This means that the function can modify the argument, what will be seen by its caller. Call-by-reference therefore has the advantage of greater time- and space-efficiency (since arguments do not need to be copied), as well as the potential for greater communication between a function and its caller (since the function can return information using its reference arguments), but the disadvantage that a function must often take special steps to "protect" values it wishes to pass to other functions. Edit: To pass by value you must do the following (in PHP5) <?php class A{ public $b = "hello"; } $a = new A(); $b = $a; $b->b = "hello from b"; echo $a->b; //prints "hello from b" because it was done by reference. $c = clone $a; $c->b = "hello from c"; echo $a->b; //prints "hello from b" because it was done by reference. echo $c->b; //print "hello from c" because it was cloned. Link to comment https://forums.phpfreaks.com/topic/128658-solved-by-ref-not-necessary/#findComment-666845 Share on other sites More sharing options...
aschk Posted October 16, 2008 Share Posted October 16, 2008 Nope. It is no longer necessary as all variables are now passed by Reference. Technically that's not correct, only object variables are passed as references NOT all variables. A string/boolean/int is passed by value as traditionally expected. i.e. $a = new A(); <- reference to object A $b = $a <- reference to object A $c = 10; $d = $b; <- copy by value NOT a reference Link to comment https://forums.phpfreaks.com/topic/128658-solved-by-ref-not-necessary/#findComment-666880 Share on other sites More sharing options...
keeB Posted October 16, 2008 Share Posted October 16, 2008 Yeah, I was talking about in the context of OOP. I'll be more clear Link to comment https://forums.phpfreaks.com/topic/128658-solved-by-ref-not-necessary/#findComment-667068 Share on other sites More sharing options...
Xeoncross Posted October 16, 2008 Share Posted October 16, 2008 In PHP 5 the way references worked in OBJECTS switched. "&" now points to the variable and NOT the variables reference. <?php class z { public $var = ''; } //Objects by reference $a = new z(); $b =& $a; $c = $a; //Return values by reference $var = $a->var; $var2 =& $a->var; //Change the value foreach(array(null, 2) as $type) { $a->var = $type; print '<pre>'; var_dump($a, $b, $c, $var, $var2); print '</pre><br />'; } //Remove object ref $a = null; print '<pre>'; var_dump($a, $b, $c, $var, $var2); print '</pre><br />'; ?> ouputs: object(z)#1 (1) { ["var"]=> &NULL } object(z)#1 (1) { ["var"]=> &NULL } object(z)#1 (1) { ["var"]=> &NULL } string(0) "" NULL object(z)#1 (1) { ["var"]=> ∫(2) } object(z)#1 (1) { ["var"]=> ∫(2) } object(z)#1 (1) { ["var"]=> ∫(2) } string(0) "" int(2) NULL NULL object(z)#1 (1) { ["var"]=> ∫(2) } string(0) "" int(2) Link to comment https://forums.phpfreaks.com/topic/128658-solved-by-ref-not-necessary/#findComment-667232 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.