Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are PHP5 objects passed by reference? [duplicate]

Tags:

php

I can't seem to get any consistent info on this. Different sources appear to say different things and the venerable php.net itself (appears) not to explicitly state this - although, I must admit, I only had a quick look.

In cases where I am passing around 'heavy' objects, I need to pass by reference, but I don't want to keep typing:

function foo(TypeName& $obj) 

if I can get away with simply

function foo(TypeName $obj) 

So what does the standard say?

like image 232
morpheous Avatar asked Apr 26 '10 16:04

morpheous


People also ask

How are PHP objects passed?

In PHP, objects are passed by references by default. Here, reference is an alias, which allows two different variables to write to the same value. An object variable doesn't contain the object itself as value. It only contains an object identifier which allows using which the actual object is found.

Are PHP variables passed by reference?

PHP variables are assigned by value, passed to functions by value and when containing/representing objects are passed by reference.

Is object passed by reference?

Object references are passed by value The reason is that Java object variables are simply references that point to real objects in the memory heap. Therefore, even though Java passes parameters to methods by value, if the variable points to an object reference, the real object will also be changed.

Are PHP arrays passed by reference?

With regards to your first question, the array is passed by reference UNLESS it is modified within the method / function you're calling. If you attempt to modify the array within the method / function, a copy of it is made first, and then only the copy is modified.


2 Answers

Objects are passed (and assigned) by reference. No need to use address of operator.

Granted what I typed is an oversimplification but will suit your purposes. The documentation states:

One of the key-points of PHP5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true. This section rectifies that general thought using some examples.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

For a more detailed explanation (explains the oversimplification as well as identifiers) check out this answer.

like image 107
webbiedave Avatar answered Sep 19 '22 15:09

webbiedave


From the PHP manual:

You can pass a variable by reference to a function so the function can modify the variable. The syntax is as follows:

<?php function foo(&$var) {     $var++; }  $a=5; foo($a); // $a is 6 here ?> 

Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);.

And from What's New in PHP5:

In PHP 5, the infrastructure of the object model was rewritten to work with object handles. Unless you explicitly clone an object by using the clone keyword you will never create behind the scene duplicates of your objects. In PHP 5, there is neither a need to pass objects by reference nor assigning them by reference

So therefore the only time you need to use the function foo(&$var) syntax is if $var might not be an instance of a class.

like image 24
Josh Avatar answered Sep 20 '22 15:09

Josh