Jump to content

[SOLVED] & (by ref), not necessary?


xtopolis

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.