Are PHP5 objects passed by reference?

I don't seem to get any consistent information about this. It seems that different sources talk about different things, and the venerable php.net itself (explicitly) does not indicate this explicitly, although, I must admit, I just looked quickly.

In those cases when I pass "heavy" objects, I need to follow the link, but I do not want to write:

function foo(TypeName& $obj) 

if i can leave just

 function foo(TypeName $obj) 

So what does the standard say?

+62
php
Apr 26 '10 at 16:17
source share
7 answers

Objects are passed (and assigned) by reference. There is no need to use the address of the operator.

Provided what I typed is a simplification, but will suit your purposes. The documentation states:

One of the key points of PHP5 OOP, which is often mentioned is that "objects are referenced by default." This is not entirely true. This resolves this general idea using some examples.

A PHP reference is an alias that allows two variables to write to the same value. Starting with PHP5, an object variable does not contain an object in itself as a value. It contains only an object identifier that allows object accessories to find the actual object. When an object is sent by argument, returned, or assigned to another variable, the various variables are not aliases: they contain a copy of the identifier that points to the same object.

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

+86
Apr 26 '10 at 16:19
source share

From the PHP manual:

You can pass the variable by reference to the function, so the function can change the variable. A syntax such as follows:

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

Note: to call a function - only by function definition. Function definitions are enough to correctly pass an argument by reference. Starting with PHP 5.3.0, you will receive a warning that the "call time by link" is out of date when used in Foo (& $ a) ;.

And from What's New in PHP5 :

In PHP 5, the object model infrastructure was rewritten to work with object handles. Unless you explicitly clone an object, using the clone keyword will never duplicate your objects behind the scene. In PHP 5 there is no need to pass objects to link or assign them Link

Therefore, therefore, the only time you need to use the syntax function foo(&$var) if $ var may not be an instance of the class.

+8
Apr 26 '10 at 16:22
source share

It seems a little more accurate, the value of the object is passed by value, but the value of the object itself is a pointer. This is different from simply following a link.

At http://www.php.net/manual/en/language.oop5.references.php the above example is good. In the first set, $ a = NULL; doesn't affect $ b since $ a was just a pointer. In the second set, $ c = NULL; calls $ d also NULL, since $ d is a reference to $ c.

 <?php class A { public $foo = 1; } $a = new A; $b = $a; $a->foo = 2; $a = NULL; echo $b->foo."\n"; // 2 $c = new A; $d = &$c; $c->foo = 2; $c = NULL; echo $d->foo."\n"; // Notice: Trying to get property of non-object... ?> 
+4
Aug 08 2018-12-12T00:
source share

Starting with PHP 5, all objects are passed and assigned by reference.

In PHP 4, you still need to specify where you want to pass the objects by reference, explicitly using the & operator.

+3
Apr 26 '10 at 16:21
source share

The short answer is yes . From Objects and Links :

One of the key points of PHP5 OOP, which is often mentioned is that "objects are referenced by default." This is not entirely true. This resolves this general idea using some examples.

A PHP reference is an alias that allows two variables to write to the same value. Starting with PHP5, an object variable does not contain an object in itself as a value. It contains only an object identifier that allows object accessories to find the actual object. When an object is sent by argument, returned, or assigned to another variable, the various variables are not aliases: they contain a copy of the identifier that points to the same object.

The important thing is that in the case when you are worried, you will never make a copy of the object unless you explicitly use the clone keyword in the function call. Whether it is an alias or an identifier, this does not change this fact.

+2
Apr 26 '10 at 16:29
source share

Yes, starting with PHP5, objects are passed by reference. There is no need to do this explicitly.

http://www.php.net/manual/en/migration5.oop.php

PHP 5 introduces a new object model. Processing of PHP objects is completely rewritten, which provides better performance and additional functions. In previous versions of PHP, objects were treated as primitive types (for example, integers and strings). The disadvantage of this method was that, semantically, the entire object was copied when assigning a variable or passed as a parameter to the method. In the new approach, objects refer to the descriptor, and not by value (you can consider the descriptor as the identifier of the object).

+1
Apr 26 '10 at 16:22
source share

Just an example in which it is useful to use "objects" by reference:

 class RefTest1 { public $foo; public function __construct(RefTest2 &$ref = null) { $this->foo =& $ref; } } class RefTest2 { public $foo; public function __construct(RefTest1 &$ref = null) { $this->foo =& $ref; } } class RefTest3 { public $foo; public function __construct(RefTest2 &$ref = null) { $this->foo =& $ref; } } class DoCrossRef { public $refTest1; public $refTest2; public function __construct() { $this->refTest1 = new RefTest1($this->refTest2); $this->refTest2 = new RefTest2($this->refTest1); } public function changeReference() { $this->refTest1 = new RefTest3($this->refTest2); } } 

At the end of RefTest1 , reference is made to RefTest2 and vice versa, also if the object RefTest2 did not exist at the time of creation of RefTest1 .

After calling DoCrossRef->changeReference() RefTest2 objects also contain a reference to the new RefTest3 object.

+1
Oct 30 '14 at 18:58
source share



All Articles