Is there ever a need to use ampersand in front of an object?
Asked Answered
H

2

9

Since objects are passed by reference by default now, is there maybe some special case when &$obj would make sense?

Hurff answered 11/1, 2014 at 4:9 Comment(1)
running on ancient php versions - the kinds that cheap/stupid hosters offer up, happily running with magic quotes and register globals turned on... the kind of installs that should be nuked from orbit.Kale
F
13

Objects use a different reference mechanism. &$object is more a reference of a reference. You can't really compare them both. See Objects and references:

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, 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.

&$object is something else than $object. I'll give you an example:

foreach ($objects as $object) {
    if ($cond) {
        $object = new Object(); // This won't affect $objects

    }
}

foreach ($objects as &$object) {
    if ($cond) {
        $object = new Object(); // This will affect $objects

    }
}

I won't answer the question if it makes sense, or if there is a need. These are opinion based questions. You can definitely live without the & reference on objects, as you could without objects at all. The existence of two mechanisms is a consequence of PHP's backward compatibility.

Flatwise answered 11/1, 2014 at 4:19 Comment(4)
If I want to modify the items in an array by looping over them. Is the second example a beter way than doing for ($objects as $key=>$value) { $objects[$key] = new Object(); } ?Thyself
The code should just point out that & is a different reference mechanism. If this is a good approach or not is out of the scope of this question.Flatwise
In PHP4, objects were not references, so objects had to be created with $obj = &new MyClass. And if you wanted to modify an object in a function, you had to pass it by reference.Insult
Thanks for the historical context. However, I think dudes who come to this question might wonder why PHP-7 still has two different reference mechanisms.Flatwise
A
1

There are situations where you add & in front of function name, to return any value as a reference.

To call those function we need to add & in front of object.

If we add & in front of object, then it will return value as reference otherwise it will only return a copy of that variable.

class Fruit() {

    protected $intOrderNum = 10;

    public function &getOrderNum() {
        return $this->intOrderNum;
    }
}

class Fruitbox() {

   public function TestFruit() {
      $objFruit = new Fruit();
      echo "Check fruit order num : " . $objFruit->getOrderNum(); // 10

      $intOrderNumber = $objFruit->getOrderNum();
      $intOrderNumber++;
      echo "Check fruit order num : " . $objFruit->getOrderNum(); // 10

      $intOrderNumber = &$objFruit->getOrderNum();
      $intOrderNumber++;
      echo "Check fruit order num : " . $objFruit->getOrderNum(); // 11
   }    
}
Anticlinal answered 28/12, 2016 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.