开发者

Where do I have to use the & operator in PHP?

A few years ago I used to work with PHP and there've been many changes in PHP, especially on the OOP part.

Back then it was necessary to use the & operator at several places, especially when working with object instances (OOP was very rudimentary then) to avoid copying object instances.

With PHP 5.2 there seems to be a lot of improvement compared to 4.0 and I'm a little bit confused.

When do I have to use the & operator in PHP to avoid any unpleasant surprises from a Java programmer's perspective?

Examples:

$a = (&开发者_JS百科;)new MyClass();    //Necessary?

function factoryMethod() {
  $a = (&)new MyClass();  //Necessary?
  return (&) $a;          //Necessary?
}

// Other cases?!

EDIT: By chance my examples don't need the & operator at all. But where would I need it?


To make it short: nowhere in your examples.

In case we're dealing with objects you do not have to use the reference operator at all.

If you're dealing with other datatypes the most common reference operations are:

Passing by Reference

function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
// $a is 6 here

Returning References

class foo {
    public $value = 42;

    public function &getValue() {
        return $this->value;
    }
}

$obj = new foo;
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
$obj->value = 2;
echo $myValue; // prints the new value of $obj->value, i.e. 2.

Looping with foreach

$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element

Be careful as the last unset()-call is important if your code uses $value again downstream.

Whereas the first case (passing by reference) is much more common than the second case, e.g. especially when dealing with arrays:

$array = array(1, 2, 3, 4);
function modifyArray(&$data)
{
    unset($data[1]);
}
modifyArrax($array);
// $array = array(0 => 1, 2 => 3, 3 => 4)


Basically don't ever use the & operator unless you need it (and try not to need it). None of your examples need it.

All objects in PHP are passed by reference. If you do this:

$arr = range(1,4);
func($arr);

function func($arr) {
  unset($arr[3]);
}

it will not modify the original array but change it to this:

function func(&$arr) {
  unset($arr[3]);
}

and it will. Whereas with objects:

class A { public $b; }

$a = new A;
$a->b = 3;
func($a);
echo $a->b; // 5

function func($a) {
  $a->b = 5;
}

Everything else is copy-on-write.

So the two circumstances where you will sometimes use the & operator are:

  1. On function arguments to pass by reference a non-object; and
  2. In a loop to modify the array.

An example of (2) is:

$arr = range(1,4);
foreach ($arr as &$v) {
  $v += 10;
}

Without the & $arr wouldn't be modified.


In PHP 5, you don't need to use the & operator in any of the places you've shown them in your example.


I don't know the correct answer... But you could take a look at the php5 manual about oop. It can be found here: http://nl2.php.net/manual/en/language.oop5.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜