3

I'm having some trouble with references in PHP. Let's say you do the following:

<?php

class foo
{
    public $str;
}

class bar
{
    public $txt;
}

class x
{
    private $objs = array();

    public function add(&$obj)
    {
        $this->objs[] = $obj;
    }

    public function update()
    {
        foreach($this->objs as $obj)
        {
            $obj = new bar();
            $obj->txt = "bar";
        }
     }
}

//Make x
$x = new x();

//Make foo
$foo = new foo();
$foo->str = "foo";

//Add
$x->add($foo);

//update
$x->update();

var_dump($foo);

The var_dump at the end gives:

class foo#2 (1) {
  public $str =>
  string(3) "foo"
}

Evidently, I'm not storing my reference properly in the array. Is there a way to do this so that $foo is of type bar after $x->update()?

2
  • Interesting. I don't think you can do this. Objects are already references. Basically you need a reference of reference. Commented Mar 16, 2014 at 2:43
  • @Ivarpoiss: "Objects are already references." That doesn't make sense. Objects are objects. Object references are pointers to objects. "Objects" are not values in PHP5. Object pointers (references) are. Object pointers are values like any other values, and variables containing them can be passed or assigned by reference as desired. Commented Mar 18, 2014 at 6:27

1 Answer 1

3

Try this. I don't know why or how this works. Just added some '&'s all over the place.

public function add(&$obj)
{
    $this->objs[] = &$obj;
}

public function update()
{
    foreach($this->objs as &$obj)
    {
        $obj = new bar();
        $obj->txt = "bar";
    }
}

UPDATE

An object is actually only logically(to us) a reference, but technically different from references in php.

PHP manual explains this. Object variables are different from references. Reference is an alias to a variable. It points to the same data held by that variable. Object is a datatype that holds object identifiers. So you can also have references to objects.

$a = new foo();
$b = $a;

$a and $b here are 2 different variables both having the same value (object id), not aliases or reference types. This works just like $a = 1;$b = $a; .

Arrays can store references. By doing var_dump($x->objs); you can see the object references in array.

You don't often see references of objects in var_dump outputs because references get constantly dereferenced by assignments. You also can't do a call-time pass-by-reference to functions like var_dump and array_push.

Your problem was caused by the dereferencing and missing the additional reference operators.

Sign up to request clarification or add additional context in comments.

1 Comment

That sure does it, I also managed to get something similar going with an in-between function, but this is pretty clean. Can anyone pipe in on some theory here, since PHP claims to not support references to references?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.