2
votes

Really simple question but rather than asking for an academic explanation I want to keep it as pragmatic as possible: when will PHP create a copy of my huge class I'm passing into a function and when will it simply create a pointer to the relevant data? And if it creates pointers all the time, what's the use of specifying & (aside from closures, obviously)? I guess I don't know enough about the guts of the interpreter.

4
What's wrong with this explanation ? php.net/manual/en/language.references.whatare.php - RobertPitt
The only thing that's wrong with said explanation is apparently the intelligence level of its audience (me) - kappasims
Try to avoid them in your code, as this feature would be deprecated in newer versions of PHP - Nazariy
@kappasims It's customary to point out that it could be a duplicate question. He posted it here because he thinks the answer you want might be in it already. - Cyclone

4 Answers

2
votes

In PHP 5, all objects are passed by their handle. Whether you pass by reference or not is irrelevant in terms of performance. (In fact, passing by reference is warned to be slower in the manual.) The object you are working on inside the function is the same object as pointed to outside the function.

When you pass an object (handle) by reference, then you can alter what the outer variable points to. This is almost always unnecessary.

0
votes

The & operator denotes a variable as being passed by reference.

$x = 'Hello World';
echo($x);

function asdf(&$var){
  $var = 'Test';
}

asdf($x);
echo($x);

Same goes for assignment and pretty much any other statement. If it isn't passed or assigned by reference, assume it is passed or assigned by value instead.

0
votes

Why bother with &, even though you can do so as please. This is how I do:

Assume I have a class 'Book' with some public methods and properties like title, author, year then to make an object of it simply:

$book = new Book()
// then I can use all public methods and properties

$book->title;

$book->author;

$book->year;

If I like to then I can make a subclass say

class Novel extends Books{

  function buildIt(Book $bk){

      $bk->title;

      // so on 

  }

}

In the function buildIt, I purposedly have an class object of Book 'parameter' in which I can pass the whole object of class 'Book'.

I hope this help.

0
votes

You can find a lot of uses of passing a variable by reference in the PHP manual. One of the best examples is preg_match.

preg_match will return the number of occurrences a pattern has been matched in the input string. It will then populate, if provided, a referenced $matches array containing the matches.

It can be seen as a way to return more than one value, although you ought to be careful with that. Per example:

class Server {

    protected $_clientId = 0;

    protected $_clients = array();

    /**
     * Get a pending connection.
     *
     * @param &$connection_id int The connection identifier.
     * @return resource The socket resource.
     */
    public function getNextClient(&$connection_id) {
        $clientSocket = socket_accept($this->_server);     
        $connection_id = $this->_clientId++; 
        $this->_clients[$connection_id] = $clientSocket;
        return $clientSocket;
    }

}

$server = new Server;
$socket1 = $server->getNextClient($id);
echo $id; // 0
$socket2 = $server->getNextClient($id);
echo $id; // 1

Important note. Objects are passed-by-reference by default. They will not be cloned. Even without specifying the & in the function argument, modifying the passed object will result in the original object being modified as well. The only way to prevent this is to clone the object in the function/method.