Question

I am studying for the Zend PHP5 Certification, everything looks good but i can not find real world examples for passing or returning variables my reference.

It would be very nice, if someone has an example when to use this?

Was it helpful?

Solution

Let's say you want to write a function that removes certain values from an array.

function remove_elements($array, $item1, $item2, ...) {
  ...
}

You could copy the array and return it. But what if you want to know if anything was removed? Instead you modify the array in-place and return the number of elements removed. So:

function remove_elements(&$array) {
  // remove elements
  return $number_removed;
}

If you create a complex structure inside your function you may want to return a reference to it rather than a copy of it. But this is a fairly marginal case because PHP uses copy-on-write (ie its not copied until it's modified) but there are some valid use cases.

Returning by reference makes more sense when you're writing a member function of a class. You could return a reference to a data member but this can also break encapsulation.

Lastly, it's worth noting that all objects are passed and returned by reference by default. Everything else is passed and returned by value.

OTHER TIPS

Pass by reference: functions like asort()

$array = array(3, 2, 1);
asort($array);

Objects in php5 are passed by reference by default. (For a correct description see http://docs.php.net/language.oop5.references)

Passing arguments by reference has already been answered...

As a (real world) example for a return value by reference pick any fluent interface where a method returns a reference to the same object as its own call-context ($this). This wouldn't work (as expected) if the return value was a copy/clone.

E.g.

class MyClass {  
  protected $foo = 'n/a';
  protected $bar = 'n/a';

  function foo($val) {
    $this->foo = (int)$val;
    return $this;
  }

  function bar($val) {
    $this->bar = (int)$val;
    return $this;
  }

  function __toString() {
    return 'foo='.$this->foo.' bar='.$this->bar;
  }
}

$o = new MyClass;
$o->foo(1)
  ->bar(2);
echo $o;

This prints foo=1 bar=2 and I think that's the result one would expect, no surprises here. If the method returned $this by value, it wouldn't work anymore. You can test that by forcing php to return a copy. Instead of the return $this lines use

return clone $this;

and the output will be

foo=1 bar=n/a

(and I wouldn't call that interface intuitive ;-))

It could be useful, if you work with chainig,like:

    public function addObject(array &$someData){
$newObj = Fabric::create($someData);
$this->listOfObj[] = $newObj;
return $newObj;
}

....
$this->addObj(array('idontknow' => 'anything'))->setName('mr.knowitall')->save();

... Sry. Same as Volkers :( (The Fluent Interface is Chaining like i know it).

Image you have a fluent Interface with an load-method. The Load-interface replace or adds some data. Here it could be useful to act with an call by reference value $success to determine that the load was successful.

Return by reference: Take some database-objects having a list of rows. The Nr. of rows points to right data of all objects (parallesl). If you return the nr. and pass it to the next object, you can skip the row in one object and skip in the others too. Always having a valide bunch of objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top