Are PHP5 objects passed by reference?

后端 未结 8 736
抹茶落季
抹茶落季 2020-11-27 03:45

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 stat

相关标签:
8条回答
  • 2020-11-27 04:28

    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.

    0 讨论(0)
  • 2020-11-27 04:29

    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.

    0 讨论(0)
  • 2020-11-27 04:37

    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 讨论(0)
  • 2020-11-27 04:41

    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).

    0 讨论(0)
  • 2020-11-27 04:42

    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.

    0 讨论(0)
  • 2020-11-27 04:48

    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.

    0 讨论(0)
提交回复
热议问题