开发者

Are PHP5 objects passed by reference? [duplicate]

This question already has answers here: Are objects in PHP assigned by value or reference? (3 answers) Closed开发者_StackOverflow中文版 2 years ago.

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?


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.


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.


It seems to be a little more precise, the value of an object is passed by value, but the value of an object itself is a pointer. This is different from just passing a reference.

On http://www.php.net/manual/en/language.oop5.references.php the example listed is nice. In the first set, $a = NULL; doesn't affect $b since $a was just a pointer. In the second set, $c = NULL; causes $d to be NULL as well 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...
?> 


As of PHP 5, all objects are passed and assigned by reference.

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


Short answer is yes. From Objects and references:

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.

What matters is that, in the case that worries you, you'll never be making a copy of an object unless you explicitly use the clone keyword in the function call. Whether it's an alias or an identifier doesn't change this fact.


Just an example where passing "objects" by reference is useful:

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 RefTest1 holds a reference to RefTest2 and the other way around, also if the RefTest2 object did not exist at the time RefTest1 was created.

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


Yes from PHP5 onwards, objects are passed by reference. No need to do that explicitly.

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

In PHP 5 there is a new Object Model. PHP's handling of objects has been completely rewritten, allowing for better performance and more features. In previous versions of PHP, objects were handled like primitive types (for instance integers and strings). The drawback of this method was that semantically the whole object was copied when a variable was assigned, or passed as a parameter to a method. In the new approach, objects are referenced by handle, and not by value (one can think of a handle as an object's identifier).


Here's another example confirming that objects are passed by reference.

<?php

// discussion: https://stackoverflow.com/questions/2715026/are-php5-objects-passed-by-reference#_=_
class A {
    public $foo = 1;
}

class B {
    function inc($obj) {
        $obj->foo++;
    }
}

$objA = new A();
$objB = new B();
$objB->inc($objA);

echo "[" . $objA->foo . "]"; // Outputs [2]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜